Is there a way to make sepcific custom fields show on a page's Wordpress admin interface only if they choose a specific template?

I want to do something like this:

Pseudo Code

if ( thisAdminPage.template === Template_Id_1 )
{
    showCustomFieldOption( "SpecialField" );
}

else hideCustomFieldOption( "SpecialField" );

Then the admin user would see a text box (or whatever kind of field it is) on that admin page. But if they changed to a different template, the field would go away.

How do I do this?

Solutions

You can use Advanced Custom Fields plugin.

Download it from here

Yes, as seen in this WPSE answer, it's just a matter of adding some jQuery to the page edit screen and watch for changes in the template dropdown.

A meta box for test purposes:

add_action( 'add_meta_boxes', function() {
    add_meta_box(
        'myplugin_sectionid',
        'Title', 
        function() {
            echo 'Test meta box';
        },
        'page'
    );
});

Then, print the script in post.php and post-new.php footer admin pages. You'll have to adjust 'onecolumn-page.php' for your Page Template name and #myplugin_sectionid for your Meta Box:

foreach( array( 'post', 'post-new' ) as $hook )
    add_action( "admin_footer-$hook.php", 'enqueue_sopt_10564' );

function enqueue_sopt_10564()
{
    if( 'page' == get_current_screen()->id ) // Correct post type
    {
        echo <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {

                /**
                 * Adjust visibility of the meta box at startup
                */
                if($('#page_template').val() == 'onecolumn-page.php') {
                    // show meta box
                    $('#myplugin_sectionid').show();
                    $("form#adv-settings label[for='myplugin_sectionid-hide']").show();
                } else {
                    // hide meta box
                    $('#myplugin_sectionid').hide();
                    $("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
                }
                /**
                 * Live adjustment of the meta box visibility
                */
                $('#page_template').on('change', function(){
                    if($(this).val() == 'onecolumn-page.php') {
                        // show meta box
                        $('#myplugin_sectionid').show();
                        $("form#adv-settings label[for='myplugin_sectionid-hide']").show();
                    } else {
                        // hide meta box
                        $('#myplugin_sectionid').hide();
                        $("form#adv-settings label[for='myplugin_sectionid-hide']").hide();
                    }
                });                 
            });    
            </script>
HTML;
    } 

}
Tags: PHP / Wordpress

Similar questions

Is there a way in wordpress that will only allow people to view the posts if they are a user?
Is there a way in wordpress that will only allow people to view the posts if they are a user? So i have a wordpress blog that I need to only make available to users that get an invite code and are registered users ...is there a way to lock down the posts and pages to logged in users
Once user sign in they are directed to a gravity form - only when it is complete will they be directed to the site
I have a survey that needs to be filled out before the user can continue onto the site. User will be logged in - this is place Fill out form - this is in place Redirect to another page - this is in place I am just not sure of the functionality always directing them to the survey if they did not do it. I have first login which is in place but this d...
Is there a multi-blog admin interface for WordPress blogs installed completely separately?
I'm an administrator of 10-20 separate WordPress blogs, and it's a big pain for me to login to all of them separately. Is there some sort of interface that allows me to do a single-sign-on administration of all of them, like there is under a WordPress MU umbrella? If so, what's it called? I don't even know the term I'd use to search for this.
Is there a way to make custom fields only editable by administrators?
I've found the following code that places custom fields into a user's profile: Is there a way to add extra functionality to this code that makes it so only administrators can edit the custom fields?
how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
I created a drop down menu under My Account, like order, account info, address...etc, how can I just redirect all visitor to login page if they are not login?
How to have when logged in users click link they get sent to one page, and when logged out users click link they get sent to a different page?
On my Wordpress website I have a page that I only want logged in users to access (e.g. "www.domain.com/logged-in-user-page") and a page that I only want logged out users to access ("www.domain.com/logged-out-user-page"). How can I have it so that if a logged in user types in the domain for the logged out page, they are directed to the logged in pag...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.