【问题标题】:Making commonly used elements in template editable使模板中的常用元素可编辑
【发布时间】:2014-07-24 22:14:32
【问题描述】:

所以我正在为客户开发一个 Wordpress 主题,并且我想赋予他们编辑模板代码中出现的许多内容的能力。诸如页脚中的单行文本或页眉中的电话号码。

我一直在寻找一个插件,它可以让我成为管理员的一部分,用于“通用元素”或类似的东西,他们可以在其中编辑简单的文本字段,我可以在其中添加一些 PHP 代码模板,因此当他们更新他们的电话号码时,它会更新他们模板中所有位置的号码。

在某种意义上有点像可编辑的“包含”。

所以我很想知道是否有人知道可以轻松做到这一点的插件 - 或者是否有人知道如何编写一些小的东西(我假设在 functions.php 文件中)可以使这成为可能。

谢谢, 亚历克斯。

【问题讨论】:

    标签: php wordpress plugins include themes


    【解决方案1】:

    为什么不添加主题选项页面?

    这将使用户能够添加/更改电话号码以及页脚文本等。

    将此添加到您的主题 function.php 文件中..

    require_once ( get_template_directory() . '/theme-options.php' );
    

    创建一个名为“theme-options.php”的新文件,它将驻留在主题文件夹中..

    复制并粘贴下面的代码...

    在 header.php 文件的顶部或要显示电话号码的地方,只需拨打电话即可获取主题选项,

    $themeOptions = get_option( 'my_theme_options' );
    

    从这里您可以使用<?php echo $themeOptions['sitetel'];?> 打印出保存的电话号码....也在您的页脚中,

    你可以把<?php echo $themeOptions['copyright'];?>

    作为额外的奖励 :) 我包含了一个简单的短代码,您可以在帖子和页面中使用它来拨打主题电话号码......

    在帖子或页面中,只需输入短代码 [tel] 即可打印出帖子中的号码。 如果,当电话号码需要更改时,只需在主题选项页面中更改它,它将在站点范围内更改..

    祝你好运……

    马蒂

    <?php
    // CUSTOM THEME OPTIONS PAGE
    add_action( 'admin_init', 'my_theme_options_init' );
    add_action( 'admin_menu', 'my_theme_options_add_page' );
    
    /**
     * Init plugin options to white list our options
    */
    function my_theme_options_init()
    {
        register_setting( 'my_theme_options', 
                          'my_theme_options', 
                          'my_theme_options_validate' );
    }
    
    /**
     * Load up the menu page
     */
    function my_theme_options_add_page() 
    {
        add_theme_page( __( 'Theme Options', 
                            'my_theme' ), 
                             __( 'Theme Options', 
                            'my_theme' ), 
                            'edit_theme_options', 
                            'my_theme_options', 
                            'my_theme_options_do_page' 
                          );
    }
    
    /**
     * Create the options page
     */
    function my_theme_options_do_page() 
    {
        global $select_options, $radio_options;
    
        if ( ! isset( $_REQUEST['settings-updated'] ) )
            $_REQUEST['settings-updated'] = false;
    
        ?>
        <div class="wrap">
            <?php echo "<h2>" . get_current_theme() . __( ' Theme Options', 'my_theme' ) . "</h2>"; ?>
    
            <?php 
            if ( false !== $_REQUEST['settings-updated'] ) : 
            ?>
                <div class="updated fade">
                    <p>
                               <strong>
                                    <?php _e( 'Theme Options saved', 'my_theme' ); ?>
                               </strong>
                            </p>
                </div>
            <?php 
            endif; 
            ?>
    
            <form method="post" action="options.php">
            <?php 
            settings_fields( 'my_theme_options' ); 
            $options = get_option( 'my_theme_options' ); 
            ?>
                      <table class="form-table">
                  <?php //SITE TELEPHONE NUMBER ?>
                <tr valign="top">
                           <th scope="row">
                             <?php _e( 'Telephone Number', 'my_theme' ); ?>
                           </th>
                            <td>
                               <input id="my_theme_options[sitetel]" class="regular-text" type="text" name="my_theme_options[sitetel]" value="<?php esc_attr_e( $options['sitetel'] ); ?>" />
                            <label class="description" for="my_theme_options[sitetel]"><?php _e( 'Website Telephone Number', 'my_theme' ); ?></label>
                            <br />
                            <small>Use the shortcode <strong>[tel]</strong> to display telephone number in posts</small> 
                        </td>
                    </tr>
                    <?php // COPYRIGHT ?>
                    <tr valign="top"><th scope="row"><?php _e( 'Copyright', 'my_theme' ); ?></th>
                        <td>
                            <input id="my_theme_options[copyright]" class="regular-text" type="text" name="my_theme_options[copyright]" value="<?php esc_attr_e( $options['copyright'] ); ?>" />
                            <label class="description" for="rehab_theme_options[copyright]"><?php _e( 'footer Copyright text', 'my_theme' ); ?></label>
                        </td>
                    </tr>
    
                </table>
    
                <p class="submit">
                    <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'my_theme' ); ?>" />
                </p>
            </form>
        </div>
        <?php 
    }
    
    
    
    /**
     * Sanitize and validate input. Accepts an array, return a sanitized array.
     */
    function my_theme_options_validate( $input ) {
    
        global $select_options, $radio_options;
    
        // Say our text option must be safe text with no HTML tags
        $input['sitetel']   = wp_filter_post_kses( $input['sitetel'] );
        $input['copyright']     = wp_filter_post_kses( $input['copyright'] );
    
        return $input;
    }
    
    
    
    
    // SHOW THEME OPTION TELEPHONE NUMBER
    function my_theme_show_tel_number()
    {
        $options = get_option( 'my_theme_options' );
        return "<strong style='color:#036;'>".$options['sitetel']."</strong>";
    }
    
    // ADD TEL SHORTCODE
    function my_theme_register_shortcodes(){
       add_shortcode('tel', 'my_theme_show_tel_number');
    }
    add_action( 'init', 'my_theme_register_shortcodes');
    ?>
    

    【讨论】:

    • 好的,这正是我想要的!我曾考虑过使用“内容块”插件——但这似乎有点矫枉过正,但这种解决方案可能是理想的。谢谢!
    • 酷...只是玩一下theme-options.php文件,根据您的需要和主题进行更改,最终应该一切顺利,您也可以添加自己的徽标/html 等在 div 包装内...显示在主题选项页面上..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    相关资源
    最近更新 更多