最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add Metabox to settingsoptions page

programmeradmin5浏览0评论

I am looking to add a metabox to an options page I have created using ACF. I have tried just about everything without success. I have created many metaboxes with custom post types but never with an options pages. Below is the code I have been trying to work with.

add_action( 'admin_init', 'adding_businessHours_shortcode' );
function adding_businessHours_shortcode( ) {

    // I have tried the to add the page with the following 
    $pages = array(
        'theme-options_page_theme_business_hours',
        'admin.php?page=theme_business_hours',
        'theme_business_hours'
    );

    add_meta_box( 'businessHoursShortcodes', __( 'Shortcodes', 'theme' ), 'displayBusinessShortcodes', $pages, 'normal', 'low' );
}

function displayBusinessShortcodes(){
    // content here
}

I am looking to add a metabox to an options page I have created using ACF. I have tried just about everything without success. I have created many metaboxes with custom post types but never with an options pages. Below is the code I have been trying to work with.

add_action( 'admin_init', 'adding_businessHours_shortcode' );
function adding_businessHours_shortcode( ) {

    // I have tried the to add the page with the following 
    $pages = array(
        'theme-options_page_theme_business_hours',
        'admin.php?page=theme_business_hours',
        'theme_business_hours'
    );

    add_meta_box( 'businessHoursShortcodes', __( 'Shortcodes', 'theme' ), 'displayBusinessShortcodes', $pages, 'normal', 'low' );
}

function displayBusinessShortcodes(){
    // content here
}
Share Improve this question edited Jan 25, 2017 at 2:13 James asked Jan 25, 2017 at 0:29 JamesJames 2053 silver badges12 bronze badges 2
  • My initial answer would have been - you can't do it because that's not how WP options pages work, but, you if really want a metabox on an options page, you CAN make it happen - though I am not sure what complications ACF may pose for you. The solution proposed and tested at this site is demonstrated in detail in the answer to a question at this site here: wordpress.stackexchange/questions/57092/… – CK MacLeod Commented Jan 25, 2017 at 2:20
  • I have read through the page you linked but that only works if I do not use ACF. It uses the same variable to declare everything. I cant find how to pull information from ACF. I have to use it because I have many fields being created using their api. – James Commented Jan 25, 2017 at 3:01
Add a comment  | 

1 Answer 1

Reset to default 0

I believe you'll need ACF Pro to make this work.

If you look in: /plugins/advanced-custom-fields-pro/pro/admin/views/html-options-page.php line: 34 - you will see how they are outputting their own metaboxes with the screen ID: "acf_options_page"

I looked through the supplied comments/links and found them confusing. Here is something simple that works with ACF Pro (put this in /child-theme/functions.php) or adapt into a plugin for your own use:

//Add ACF Options Page
if( function_exists('acf_add_options_page') ) {
    
    acf_add_options_page( array(
        'page_title'  => 'My Options Page',
        'menu_title'  => 'My Options Page',
        'capability' => 'manage_options',
        'menu_slug'   => 'my-acf-options-page',
    ) );

}

//Add the metabox if we're viewing our options page
add_action('admin_init', function() {
    if ($_GET['page'] !== 'my-acf-options-page') return;
    add_meta_box( 'my-options-page-metabox', "Metabox title", 'my_options_page_content_callback', 'acf_options_page', 'normal', 'high' );
});

//Callback for outputting metabox content
function my_options_page_content_callback() {
    ?>
    <h2>Your metabox content goes here</h2>
    <?php
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论