添加Metaboxes--这么多代码,有没有更短的干法?

时间:2015-10-17 作者:Christina

我在函数中添加了以下两个元数据库。php文件,它可以工作,但需要大量代码。

问:没有插件,有没有一种更短或更简单的方法可以做到这一点

enter image description here

/**
 * Add custom fields to a specific WP page template & save it
*/

$post_id = $_GET[\'post\'] ? $_GET[\'post\'] : $_POST[\'post_ID\'] ;
$template_file = get_post_meta($post_id,\'_wp_page_template\',TRUE);
  // check for a template type
  if ($template_file == \'template-testhome.php\') { //this is the template change on real site

  // add the actions to add functions :: how many slides to show
   add_action( \'admin_menu\', \'slidestoshow_create_post_meta_box\' );
   add_action( \'save_post\', \'slidestoshow_save_post_meta_box\', 10, 2 );
   

  // add the actions to add functions :: type of slider infinite or not
   add_action( \'admin_menu\', \'slidetype_create_post_meta_box\' );
   add_action( \'save_post\', \'slidetype_save_post_meta_box\', 10, 2 );


//begin slides to show meta box
function slidestoshow_create_post_meta_box() {
    add_meta_box( \'slidestoshow_meta_box\', \'Slides to Show & Scroll (1-6)\', \'slidestoshow_page_meta_box\', \'page\', \'normal\', \'high\' );
}

function slidestoshow_page_meta_box( $object, $box ) { ?>
    <p>
        <label for="slides-to-show-scroll">Slides to Show & Scroll (1-6)</label>
        <!-- keep reference
        <textarea name="second-excerpt" id="second-excerpt" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo wp_specialchars( get_post_meta( $object->ID, \'Second Excerpt\', true ), 1 ); ?></textarea>
        -->
       <input type="text" id="slides-to-show-scroll" name="slides-to-show-scroll" value="<?php echo wp_specialchars( get_post_meta( $object->ID, \'Slides to Show Scroll\', true ), 1 ); ?>" size="10" />
       <input type="hidden" name="slidestoshow_page_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    </p>
<?php }
// end create the meta box for slides to show


//begin slide type meta box
function slidetype_create_post_meta_box() {
    add_meta_box( \'slidetype_meta_box\', \'Infinte (true or false)\', \'slidetype_page_meta_box\', \'page\', \'normal\', \'high\' );
}

function slidetype_page_meta_box( $object, $box ) { ?>
    <p>
        <label for="slide-type">Infinite (true or false)</label>
       <input type="text" id="slide-type" name="slide-type" value="<?php echo wp_specialchars( get_post_meta( $object->ID, \'Slide Type\', true ), 1 ); ?>" size="10" />
       <input type="hidden" name="slidetype_page_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
    </p>
<?php }
// end create the meta box for slides type


function slidestoshow_save_post_meta_box( $post_id, $post ) {

    if ( !wp_verify_nonce( $_POST[\'slidestoshow_page_meta_box_nonce\'], plugin_basename( __FILE__ ) ) )
        return $post_id;

    if ( !current_user_can( \'edit_post\', $post_id ) )
        return $post_id;

    $meta_value = get_post_meta( $post_id, \'Slides to Show Scroll\', true );
    $new_meta_value = stripslashes( $_POST[\'slides-to-show-scroll\'] );

    if ( $new_meta_value && \'\' == $meta_value )
        add_post_meta( $post_id, \'Slides to Show Scroll\', $new_meta_value, true );

    elseif ( $new_meta_value != $meta_value )
        update_post_meta( $post_id, \'Slides to Show Scroll\', $new_meta_value );

    elseif ( \'\' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, \'Slides to Show Scroll\', $meta_value );
}
//end the save for slides to show



function slidetype_save_post_meta_box( $post_id, $post ) {

    if ( !wp_verify_nonce( $_POST[\'slidetype_page_meta_box_nonce\'], plugin_basename( __FILE__ ) ) )
        return $post_id;

    if ( !current_user_can( \'edit_post\', $post_id ) )
        return $post_id;

    $meta_value = get_post_meta( $post_id, \'Slide Type\', true );
    $new_meta_value = stripslashes( $_POST[\'slide-type\'] );

    if ( $new_meta_value && \'\' == $meta_value )
        add_post_meta( $post_id, \'Slide Type\', $new_meta_value, true );

    elseif ( $new_meta_value != $meta_value )
        update_post_meta( $post_id, \'Slide Type\', $new_meta_value );

    elseif ( \'\' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, \'Slide Type\', $meta_value );
}
//end the save for slide type

1 个回复
最合适的回答,由SO网友:Landing on Jupiter 整理而成

Leverage Functions

您可以将冗余代码提取到可重用函数中。例如,最后两个函数可以这样重写。我得到的代码实际上长了2行,但从调试的角度来看,它要干净得多。而且,如果有第三个自定义字段,这种方法肯定会更短。

function slidestoshow_save_post_meta_box( $post_id, $post ) {

    $nonce = \'slidestoshow_page_meta_box_nonce\';
    $meta_label = \'Slides to Show Scroll\';
    $meta_slug = \'slides-to-show-scroll\';

    if ( !my_save_post_met_box( $post_id, $post, $meta_label, $meta_slug, $nonce ) ) {
        return $post_id;
    }
}

function slidetype_save_post_meta_box( $post_id, $post ) {

    $nonce = \'slidetype_page_meta_box_nonce\';
    $meta_label = \'Slide Type\';
    $meta_slug = \'slide-type\';

    if ( !my_save_post_met_box( $post_id, $post, $meta_label, $meta_slug, $nonce ) ) {
        return $post_id;
    }
}

function my_save_post_meta_box( $post_id, $post, $meta_label, $meta_slug, $nonce ) {

    if ( !wp_verify_nonce( $_POST[$nonce], plugin_basename( __FILE__ ) ) )
        return;

    if ( !current_user_can( \'edit_post\', $post_id ) )
        return;

    $meta_value = get_post_meta( $post_id, $meta_label, true );
    $new_meta_value = stripslashes( $_POST[$meta_slug] );

    if ( $new_meta_value && \'\' == $meta_value )
        add_post_meta( $post_id, $meta_label, $new_meta_value, true );

    elseif ( $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_label, $new_meta_value );

    elseif ( \'\' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_label, $meta_value );
}
而且,您可以通过将nonce重命名为与slug相同的方案来回收两行代码。类似这样:

if ( !wp_verify_nonce( $_POST[$meta_slug . \'_page_meta_box_nonce\'], plugin_basename( __FILE__ ) ) )

Use Arrays and Foreach Loops

你的动作挂钩可以这样优化。同样,更多的代码行—只有两个自定义字段—但更易于阅读和调试。

$fnc_labels = array( \'slidestoshow\', \'slidetype\' );

foreach ( $fnc_labels AS $fnc_label ) {
    add_action( \'admin_menu\', $fnc_label . \'_create_post_meta_box\' );
    add_action( \'save_post\', $fnc_label . \'_save_post_meta_box\', 10, 2 );
}

相关推荐

保存Metabox内容无效

我正在尝试保存一个metabox输入,但它似乎不起作用。我使用的是数组(因为我需要我的metabox有60行),所以我假设问题就出在数组中。这是我为管理员提供的metabox函数(它可以正确显示我要显示的信息):function mock_metabox() { global $post; // Nonce field wp_nonce_field( basename( __FILE__ ), \'mock_fields\' ); // in