Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been attempting to build a plugin(my very first) that adds some functionality for use with the Custom Header admin screen.

I would like to have my plugin add an anchor link to the Appearance >> Header admin screen, and have the link trigger a function in my main plugin file: but, I'm having a lot of trouble figuring out how to manipulate the custom header admin screen(well, any admin screen honestly).

I am using the new add_theme_support('custom-header') method of adding the custom header functionality for wordpress 3.4+, for now: and, I THINK I need to build a function(that adds the anchor link) and trigger it with the 'admin-head-callback' argument in my add_theme_support method. Assuming that much is correct(?), can anyone provide an example of how to add html to an existing admin screen, from a plugin, please?

share|improve this question
    
If I'm missing some context or something please let me know, I am happy to provide any other info that I can of course. –  Ben Aug 29 '12 at 21:48

1 Answer 1

up vote 0 down vote accepted

Ok, after a bunch of stabbing in the dark, I have successfully added a link to the Appearance >> Header admin screen: and, when clicked on, the link triggers a jquery .post() ajax request that calls a function in my main plugin file. I am going to share a basic example of what I did so as not to divulge my secret plugin functionality ;)

The first part was getting a link into the admin screen. Originally I thought there might be a wordpress way to do this, but I ended up using jquery to actually write the link when I activate the Custom Header functionality with add_theme_support() 'admin-head-callback'. It works like this:

add_theme_support('custom-header', array(
                    'default-image' => get_template_directory_uri() . '/images/headers/shore.jpg',
                    'width' => 1000,
                    'height' => 288,
                    'header-text' => false,
                    'uploads' => true,
                    'admin-head-callback' => 'customThemeSetup_admin_header' // run callback function in admin head of custom header screen
                ));

Next up I had to create the above callback function and make it add a jquery script that writes an anchor link into the custom header admin screen. The link uses JQuery's .post() method to send a request to wordpress:

function customThemeSetup_admin_header()
{
        $myPluginFile = plugin_dir_path(__DIR__);

        <script type="text/javascript" charset="utf-8">
                jQuery(document).ready(function($){
                    $('.available-headers:last').append("<a id='testLink' href='#'>Trigger plugin function with AJAX</a>");
                    $('#testLink').click(function(e){
                        e.preventDefault();

                        $.post(ajaxurl, {
                            action: 'test_ajax' // the action for triggering plugin function will be wp_ajax_test_ajax
                        }, function(response, status){
                            if(status == "success")
                            {
                                 alert(response);// returns text in die() at the end of my plugin function(...next step)
                            }
                        });
                    });

                });
            </script>
 }

Finally I needed to tell wordpress to run my plugin function, when it detects the ajax request above with action: 'test_ajax'. That looks like this:

add_action('wp_ajax_test_ajax', 'testAjax');// when wordpress detects the action wp_ajax_test_ajax, it will trigger my plugin function testAjax() below
function testAjax()
{
die("Function triggered with AJAX!");// text sent back to alert(response) from previous step
}

I can see that the reason I got no help was that the process was broader than I had originally imagined: but, hopefully this helps someone else in the future. Please feel free to suggest, or add any modifications as this was my first time doing such a thing.

share|improve this answer
    
Make sure to mark this as the answer when you are able. –  Brian Fegter Sep 5 '12 at 1:49
    
Thanks for the reminder Brian! –  Ben Sep 5 '12 at 1:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.