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.

Note: People are assuming this question is already answered when in fact it is no where close to being the right answer.

I have created tons of short codes, How ever I am using Jquery plugin TOC and I would like to create a short code for it, I am not sure where to begin. I would assume you would follow standard procedure for creating one, but I have never had one spit out javascript before.

The only code I have to show you is, my short code:

if(! function_exists ('aisis_toc') ){
    function aisis_toc($atts, $content = null){
        return '
        <script type="text/javascript" charset="utf-8">
            $(function(){ $("#toc").tableOfContents(); })
        </script>';
    }
}

I have registered the hort code as toc, and tried doing [toc] but alas nothing happens. What am I doing wrong? I don't need or shouldn't need ajax of any kind. this should be easy.

I should also state that this short code should only get the h1-6 tags that are in the post them selves and not on the page in general.

share|improve this question
    
See also wordpress.stackexchange.com/a/73009/73 –  toscho Mar 26 '13 at 19:27
    
no thats not what I want. –  TheWebs Mar 26 '13 at 19:35
    
Please explain why you cannot return a string with your JavaScript and why you have to use an inline-script at all. –  toscho Mar 26 '13 at 20:03
    
I can see this scenario. You would enqueue the library but still need to run jQuery.do_stuff(some, parameters); I would consider an inline script to be preferable to enqueueing another file, for that case. –  s_ha_dum Mar 26 '13 at 20:07
    
@toscho UPDATED THE POST. Check it out. theres your code sample. –  TheWebs Mar 26 '13 at 20:41
show 1 more comment

1 Answer 1

Put the script into an external file, set the selector there and enqueue it from your shortcode:

if(! function_exists ('aisis_toc') )
{
    function aisis_toc() 
    {
        if ( ! is_singular() )
            return;

        wp_enqueue_script(
        'jquery-toc',
        plugins_url( 'js/jquery-toc.js', __FILE__ ),
        array ( 'jquery' ),
        NULL,
        TRUE);

        return;
    }
}
share|improve this answer
add comment

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.