自定义元框(选择)不保存

时间:2019-01-08 作者:Robert L

我正在尝试创建一个元框,其中包含使用主页模板的每个页面的选择选项。php分配给它。我已使该功能正常工作。

我还希望它位于编辑器上方,页面标题下方,这是可行的,但现在不行了。我一直在修改代码,试图让它正常工作。不确定是什么原因导致它在编辑器上方不可用。它过去是“高级”而不是“向上”,现在两者都不起作用了。

我最大的问题是,在页面更新中,页面没有保存/存储所做的选择。我想做的是获取所选选项的页面ID,并在页面上的变量中使用它。php。页面ID被分配给每个选项的“值”。这就是我计划或假设获取页面id的方式。然后,页面id需要传递给一个变量,我将使用该变量获取页面上的高级自定义字段值。php。

下面是函数的代码。php:

/*   ** START ** ADDS A NEW META BOX TO PAGES   */
// register the meta box
add_action( \'add_meta_boxes\', \'my_custom_field_cityselect\' );
add_action( \'load-page.php\', \'my_custom_field_cityselect\' );
add_action( \'load-page-new.php\', \'my_custom_field_cityselect\' );
function my_custom_field_cityselect() {
    add_meta_box(
        \'my_select_meta_box_id\',          // this is HTML id of the box on edit screen
        \'Select The City For This Page\',    // title of the box
        \'my_customfield_box_content\',   // function to be called to display the checkboxes, see the function below
        \'page\',        // on which edit screen the box should appear
        \'up\',      // part of page where the box should appear
        \'high\'      // priority of the box
    );
}

// Move all "up" metaboxes above the default editor
function my_upper_meta_box() {     global $post, $wp_meta_boxes;
// Get the globals
do_meta_boxes( get_current_screen(), \'up\', $post );
// Output the "up" meta boxes 

} add_action( \'edit_form_after_title\', \'my_upper_meta_box\' );

// display the metabox
function my_customfield_box_content($post) {
    // nonce field for security check, you can have the same
    // nonce field for all your meta boxes of same plugin
    wp_nonce_field( basename( __FILE__ ), \'my_custom_field_select_nonce\' );
    $meta_element_class = get_post_meta($post->ID, \'my_customfield_box_content\', true);
    $args = array(
        \'post_type\' => \'page\',
        \'posts_per_page\' => -1,
        \'meta_query\' => array(
            array(
                \'key\' => \'_wp_page_template\',
                \'value\' => \'page-home-template.php\'
            )
        )
    );
    $the_pages = new WP_Query( $args );
    echo \'<select style=\\\'width:300px;\\\' name=\\\'my_metabox_location\\\'>\';
    echo \'<option style=\\\'color:red\\\' value=\\\'0\\\'>Choose Appropriate Location</option>\';
    if( $the_pages->have_posts() ){
        while( $the_pages->have_posts() ){
            $the_pages->the_post();
            $mymbpostid = get_the_ID();
            $mymbposttitle = str_replace("Weight Loss ","",get_the_title());
            $mymbposttitle = str_replace("Home","(HOME PAGE) - DO NOT USE THIS OPTION",get_the_title());
            echo \'<option style=\\\'color:green\\\' value=\\\'\'. $mymbpostid .\'\\\'\'. selected( $meta_element_class, \'$mymbpostid\' ) .\'>\'. $mymbposttitle .\'</option>\';
        }
    }
    wp_reset_postdata();

    echo \'</select>\';
}

// save data from checkboxes
add_action( \'save_post\', \'my_custom_field_data\' );
function my_custom_field_data($post_id) {

    // check if this isn\'t an auto save
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
        return;

    // security check
    if ( !wp_verify_nonce( $_POST[\'mypluing_nonce\'], plugin_basename( __FILE__ ) ) )
        return;

    // further checks if you like, 
    // for example particular user, role or maybe post type in case of custom post types

    // now store data in custom fields based on checkboxes selected
    if ( isset( $_POST[\'my_metabox_location\'] ) )
    {
        $meta_box_dropdown_value = $_POST["my_metabox_location"];
    }  
    update_post_meta( $post->ID, \'my_customfield_box_content\', $_POST[\'meta_box_dropdown_value\'] );
}
// Removes the Meta Box from the Home Page Template
add_action(\'admin_head\',\'my_meta_init\');
function my_meta_init(){
    $template_file = get_post_meta(get_the_ID(), \'_wp_page_template\', TRUE);

    if (($template_file == \'page-home-template.php\')) {
        remove_meta_box(\'my_select_meta_box_id\',\'page\',\'advanced\');    
    }
}

// Add styles for meta box
add_action(\'admin_head\', \'custom_admin_meta_css\');          
function custom_admin_meta_css() {
  echo \'<style>#my_select_meta_box_id h2 {color:red}</style>\';
}
/*   ** END ** ADDS A NEW META BOX TO PAGES   */
这是页面的代码。php(不确定第二行是否正在执行任何操作或是否需要执行):

<?php 
$loc_phone = get_field(\'location_phone_number\', $mymbpostid);
$loc_info = get_post_meta( $post->ID,\'my_custom_field_checkboxes\',true );
echo \'<h2>Page ID - \'. $loc_info .\' | Page Phone - \'. $loc_phone .\'</h2>\';
?>
我很抱歉,如果这是一个热混乱的混乱,因为我已经抓住了许多帖子的碎片,并将其拼凑在一起。我觉得我真的很接近,但就是无法挽救。此外,也不确定是否将其设置为单独为每个页面保存,因为它确实需要这样做。

非常感谢您的帮助。正确的代码示例对我来说是最好的答案,因为我的PHP和WordPress核心知识非常有限。提前感谢您!

1 个回复
SO网友:Robert L

因此,我想出了一个解决办法,将其切碎,并将在线找到的其他解决方案的部分放入其中。这是我正确使用的代码。功能。php:

/*   ** START ** ADDS A NEW META BOX TO PAGES   */
// register the meta box
add_action( \'add_meta_boxes\', \'my_custom_field_cityselect\' );
add_action( \'load-page.php\', \'my_custom_field_cityselect\' );
add_action( \'load-page-new.php\', \'my_custom_field_cityselect\' );
function my_custom_field_cityselect() {
    add_meta_box(
        \'my_select_meta_box_id\',          // this is HTML id of the box on edit screen
        \'Select The City For This Page\',    // title of the box
        \'my_customfield_box_content\',   // function to be called to display the select, see the function below
        \'page\',        // on which edit screen the box should appear
        \'normal\',      // part of page where the box should appear
        \'high\'      // priority of the box
    );
}

// display the metabox
function my_customfield_box_content($post) {
    // nonce field for security check, you can have the same
    // nonce field for all your meta boxes of same plugin
    global $post;
    // storing the global post object so it doesn\'t get mixed up with the options
    $post_old = $post;

    $custom = get_post_custom($post->ID);
    if (isset($custom["meta_element_class"][0])) {
    $meta_element_class = $custom["meta_element_class"][0];
    } else {
    $meta_element_class = \'0\';
    }
    $args = array(
        \'post_type\' => \'page\',
        \'posts_per_page\' => -1,
        \'meta_query\' => array(
            array(
                \'key\' => \'_wp_page_template\',
                \'value\' => \'page-home-template.php\'
            )
        )
    );
    $posts = get_posts($args);
    echo \'<select style=\\\'width:300px;\\\' name=\\\'my_metabox_location\\\' id=\\\'my_metabox_location\\\'>\';
    echo \'<option style=\\\'color:red\\\' value=\\\'0\\\'\'. selected( $meta_element_class, \'0\' ) .\'>Choose Appropriate Location</option>\';
    foreach( $posts as $post ) : setup_postdata($post);
        $mymbpostid = $post->ID;
        $mymbposttitle = str_replace("Weight Loss ","",get_the_title());
        $mymbposttitle = str_replace("Home","(HOME PAGE) - DO NOT USE THIS OPTION",get_the_title());
        echo \'<option style=\\\'color:green\\\' value=\\\'\'. $mymbpostid .\'\\\'\'. selected( $meta_element_class, $mymbpostid ) .\'>\'. $mymbposttitle .\'</option>\';
    endforeach;

    echo \'</select>\';
    // restoring the global post object
    $post = $post_old;
    setup_postdata( $post );
}

// save data from select
add_action( \'save_post\', \'my_custom_field_data\' );
function my_custom_field_data() {
    global $post;
    // check if this isn\'t an auto save
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
        return;


    // further checks if you like, 
    // for example particular user, role or maybe post type in case of custom post types

    // now store data in custom fields based on select option
    if ( isset( $_POST[\'my_metabox_location\'] ) )
    {
        update_post_meta( $post->ID, \'meta_element_class\', $_POST[\'my_metabox_location\'] );
    }
}
// Removes the Meta Box from the Home Page Template
add_action(\'admin_head\',\'my_meta_init\');
function my_meta_init(){
    $template_file = get_post_meta(get_the_ID(), \'_wp_page_template\', TRUE);

    if (($template_file == \'page-home-template.php\')) {
        remove_meta_box(\'my_select_meta_box_id\',\'page\',\'advanced\');    
    }
}

// Add styles for meta box
add_action(\'admin_head\', \'custom_admin_meta_css\');          
function custom_admin_meta_css() {
  echo \'<style>#my_select_meta_box_id h2 {color:red}</style>\';
}
/*   ** END ** ADDS A NEW META BOX TO PAGES   */
以及页面中的此代码。php:

<?php $meta_element_class = get_post_meta($post->ID, \'meta_element_class\', true); ?>
<?php $loc_phone = get_field(\'location_phone_number\', $meta_element_class); ?>
<?php echo \'<h2>Page ID - \'. $meta_element_class .\' | Page Phone - \'. $loc_phone .\'</h2>\'; ?>
随着WordPress 5.0.3和经典编辑器插件的安装,我仍然无法在内容编辑器上方显示元框。如果有人知道如何做到这一点,我们将不胜感激。

相关推荐

WordPress网站损坏,链接失效,无法访问wp-admin页面

我刚刚收到一个坏掉的Wordpress网站,我现在负责管理这个网站。编辑:自从发布这篇文章以来,我对自己的问题有了一些新的发现。当尝试访问Wordpress管理页面(通过单击登录仪表板的链接)时,我得到一个404未找到页面。此404错误页面与所托管网站上的所有其他页面具有相同的品牌(背景和样式)。我猜我的链接断了。网站上的身份验证页面不起作用,我无法访问前面所述的管理门户。我有权访问根目录和wp content文件夹。该页面由wordpress托管,我通过启用sftp访问root。我还可以访问phpMyA