Leave a Reply

2 comments

  1. I did something like this once, but when you clicked on a certain category (essentially the same) It certainly isn’t a true ajax solution, it just hides and reveals the div with the settings, but It is a solution which works. You’ll definitely have to modify this for your needs, but if you have a grasp on jQuery, I’m sure you’ll be able to modify this for your needs. If you are a little more specific about your needs, I’d be happy to edit this up to suit what you are looking for more exactly.

    The Code:

    I used this in a plugin, but you can just put this into your theme’s functions.php

    function customadmin_testimonial() {
        if ( is_admin() ) {
            $script = <<< EOF
    <script type='text/javascript'>
        jQuery(document).ready(function($) {
            $('#testimonial-information').hide();
            $('#in-category-3').is(':checked') ? $("#testimonial-information").show() : $("#testimonial-information").hide();
            $('#in-category-3').click(function() {
                $("#testimonial-information").toggle(this.checked);
            });
        });
    </script>
    EOF;
            echo $script;
        }
    }
    add_action('admin_footer', 'customadmin_testimonial');
    

    Basically what you have here is a jQuery script that initially hides a meta box that I have already set up. The box’s ID is #testimonial-information. Then it checks on whether or not the specific category’s box is checked, and if it is, shows it. Then it listens for a click on the specific category’s box and toggles it’s visibility.

    The Result:

    A meta box that is visible only when the user has chosen a specific category. All you’ll need to do is set up your metaboxes and get all the ID’s of the elements you need. You’ll need the ID’s of the metaboxes as well as the checkboxes in question. Then all you need to do is follow this formula to get what you are looking for.

    If you have everything set up but are having a hang-up writing the javascript, just provide me the ID’s of the metaboxes and corresponding checkboxes and I’d be more than happy to write it up for you.

  2. This is the final javascript function. It should be hooked into admin_footer hook.

    /**
     * jQuery show/hide for meta box, post editor meta box
     * 
     * Hides/Shows boxes on demand - depending on your selection inside the post formats meta box
     */
    function wpse14707_scripts()
    {
        wp_enqueue_script( 'jquery' );
    
        $script = '<<< EOF
        <script type="text/javascript">
            jQuery( document ).ready( function($)
                {
                    $( "#post_format_box" ).addClass( "hidden" );
    
                    $( "input#post-format-0" ).change( function() {
                        $( "#postdivrich" ).removeClass( "hidden" );
                        $( "#post_format_box" ).addClass( "hidden" );
                    } );
    
                    $( "input:not(#post-format-0)" ).change( function() {
                        $( "#postdivrich" ).addClass( "hidden" );
                        $( "#post_format_box" ).removeClass( "hidden" );
                    } );
    
                    $( "input[name="post_format"]" ).click( function() {
                        var mydiv = $(this).attr( "id" ).replace( "post-format-", "" );
                        $( "#post_format_box div.inside div" ).addClass("hidden");
                        $( "#post_format_box div.inside div#"+mydiv).removeClass( "hidden" );
                    } );
                }
            );
        </script>
        EOF';
    
        return print $script;
    }
    add_action( 'admin_footer', 'wpse14707_scripts' );