简体   繁体   English

如何为wordpress主题选项执行动态功能

[英]How to do a dynamic function for a wordpress theme options

I'm working on a wordpress theme, and I'm wondering how to do a more clean code when displaying options in wp-admin. 我正在研究wordpress主题,并且想知道在wp-admin中显示选项时如何做更干净的代码。

Right now, my code is the following: 现在,我的代码如下:

...    

<?php $field_key = 'logo_retina'; if ( ! isset( $options[$field_key] ) ) $options[$field_key] = '';?>
                <tr valign="top"><th scope="row"><?php _e( 'Logo HQ', 'yes' ); ?></th>
                    <td><input id="<?php echo $field_key; ?>" class="regular-text" type="text" name="yes_theme_options[<?php echo $field_key; ?>]" value="<?php esc_attr_e( $options[$field_key] ); ?>" /></td>
                </tr>

            <?php $field_key = 'logo_non_retina'; if ( ! isset( $options[$field_key] ) ) $options[$field_key] = ''; ?>
                <tr valign="top"><th scope="row"><?php _e( 'Logo SQ', 'yes' ); ?></th>
                    <td><input id="<?php echo $field_key; ?>" class="regular-text" type="text" name="yes_theme_options[<?php echo $field_key; ?>]" value="<?php esc_attr_e( $options[$field_key] ); ?>" /></td>
                </tr>

            <?php $field_key = 'slogan_retina'; if ( ! isset( $options[$field_key])) $options[$field_key] = ''; ?>
                <tr valign="top"><th scope="row"><?php _e( 'Slogan HQ', 'yes' ); ?></th>
                    <td><input id="<?php echo $field_key; ?>" class="regular-text" type="text" name="yes_theme_options[<?php echo $field_key; ?>]" value="<?php esc_attr_e( $options[$field_key] ); ?>" /></td>
                </tr>

...

The original code is more than this three forms. 原始代码不只是这三种形式。

What I want to do is display this options, but dynamically. 我想做的是显示此选项,但要动态显示。 I mean, with just one function in php, for example. 我的意思是,例如,在php中只有一个功能。 I can't figure out how to do this. 我不知道该怎么做。

Here's the function I use when defining custom options for a theme. 这是定义主题的自定义选项时使用的功能。

To add a dynamic field to the custom options, you add to the $options array with the following fields: 要将动态字段添加到自定义选项,请使用以下字段将添加到$ options数组中:

"name" is the display that will show up in the admin panel. “名称”是将显示在管理面板中的显示。 For example "Facebook URL" 例如“ Facebook URL”

"desc" is the text that gets displayed bellow the field. “ desc”是在该字段下显示的文本。 For example "The full HTTP URL to your Facebook page" 例如“ Facebook页面的完整HTTP URL”

"id" is the variable you're going to use in your theme when you're getting the settings values. “ id”是您在获取设置值时将在主题中使用的变量。

"type" is the type of field that is going to get displayed. “类型”是将要显示的字段的类型。 These options can be "text" for a text field, "textarea" for a textarea field or "heading" for a heading break. 这些选项可以是用于文本字段的“文本”,用于文本字段的“ textarea”或用于标题中断的“ heading”。 The heading break lets you categorize the different fields. 标题分隔符使您可以对不同的字段进行分类。

Only "name" and "id" are required. 只需要“名称”“ id”

$options =  
array( 
    array("name" => " Options", "type" => "title"),
    array("type" => "open"), 
    array("name" => "", "desc" => "", "id" => "", "type" => "text"),
    array( "type" => "close")
);  

function mytheme_add_admin() {

    global $shortname, $options;

    if($_GET['page'] == basename(__FILE__)) {
        if($_REQUEST['action'] == 'save') {

            foreach($options as $value) {
                update_option( $value['id'], $_REQUEST[ $value['id'] ] ); 
            }

            foreach($options as $value) {
                if(isset($_REQUEST[ $value['id']])) { 
                    update_option($value['id'], $_REQUEST[$value['id']]); 
                } else { 
                    delete_option( $value['id'] ); 
                } 
            }

            header("Location: themes.php?page=functions.php&saved=true");
            die;

        } else if($_REQUEST['action'] == 'reset') {

            foreach($options as $value) {
                delete_option($value['id']); 
            }

            header("Location: themes.php?page=functions.php&reset=true");
            die;

        }
    }

    //add_menu_page(get_bloginfo('name') . " Options", "Theme Settings", 'administrator', basename(__FILE__), 'mytheme_admin', '', '63.3');
    add_submenu_page('themes.php', get_bloginfo('name') . " Options", 'Theme Settings', 'administrator', basename(__FILE__), 'mytheme_admin');

}

function mytheme_admin() {
    global $shortname, $options;
    if($_REQUEST['saved']) echo '<div id="message" class="updated fade"><p><strong>'.get_bloginfo('name').' settings saved.</strong></p></div>';
    if($_REQUEST['reset']) echo '<div id="message" class="updated fade"><p><strong>'.get_bloginfo('name').' settings reset.</strong></p></div>';
?>

<div class="wrap">
<h2><?php echo get_bloginfo('name'); ?> Settings</h2>

    <form method="post" style="background-color:#f8f8f8; padding:10px; margin-top: 10px;">

    <?php foreach ($options as $value) {
    switch ( $value['type'] ) {

    case "open":
    ?>
    <table width="100%" border="0">

    <?php break;

    case "close":
    ?>

    </table>

    <?php break;

    case 'text':
    ?>


    <tr>
    <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
    <td width="80%"><input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings($value['id'])); } else { echo stripslashes($value['std']); } ?>" /></td>
    </tr>

    <tr>
    <td><small><?php echo $value['desc']; ?></small></td>
    </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>

    <?php
        break;
        case 'heading':
    ?>

    <tr>
        <td width="100%" colspan="2" valign="middle"><h3><?php echo $value['name']; ?></h3></td>
    </tr>

    <?php
    break;

    case 'textarea':
    ?>

    <tr>
    <td width="20%" rowspan="2" valign="middle"><strong><?php echo $value['name']; ?></strong></td>
    <td width="80%"><textarea name="<?php echo $value['id']; ?>" style="width:400px; height:200px;" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings($value['id'])); } else { echo stripslashes($value['std']); } ?></textarea></td>

    </tr>

    <tr>
    <td><small><?php echo $value['desc']; ?></small></td>
    </tr><tr><td colspan="2" style="margin-bottom:5px;border-bottom:1px dotted #000000;">&nbsp;</td></tr><tr><td colspan="2">&nbsp;</td></tr>    
    <?php break;

    }
    }
    ?>

    <input name="save" type="submit" class="button-primary" value="Save changes" />
    <input type="hidden" name="action" value="save" />
    </form>

    <?php
    }


add_action('admin_menu', 'mytheme_add_admin');

Here's how you get the settings value from your custom settings: 从自定义设置中获取设置值的方法如下:

<?php
    global $options;
    foreach ($options as $value) {
        if(get_settings($value['id']) === FALSE) { $$value['id'] = stripslashes($value['std']); } else { $$value['id'] = stripslashes(get_settings($value['id'])); }
    }
?>

You place that on the top of the file you need to get the settings. 您将其放置在文件顶部,以获取设置。 The variables will be the same as the "ID" you defined above. 变量将与您上面定义的“ ID”相同。 If the ID is " facebook ", the PHP variable will then be $facebook . 如果ID为“ facebook ”,则PHP变量将为$ facebook

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM