• Resolved Bloke

    (@bloke)


    I am trying to follow this tutorial explaining how to add ajaxurl. Its seems to be a really simple example. What can I do to find why its not working? I looked at the source code and its adding the script to the header. Clicking on “view our site description” doesn’t do anything.
    …./wp/wp_ajax

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter Bloke

    (@bloke)

    function add_ajaxurl_cdata_to_front(){ ?>
        <script type="text/javascript"> //<![CDATA[
            ajaxurl = '<?php echo admin_url( 'admin-ajax.php'); ?>';
        //]]> </script>
    <?php }
    add_action( 'wp_head', 'add_ajaxurl_cdata_to_front', 1);
    
    add_action( 'wp_footer', 'add_js_to_wp_footer' );
    function add_js_to_wp_footer(){ ?>
        <script type="text/javascript">
        jQuery('#view_site_description').click(function(){
            jQuery.ajax({
                type: 'POST',
                url: ajaxurl,
                data: {"action": "view_site_description"},
                success: function(data){alert(data);}
            });
            return false;
        });
        </script>
    <?php }
    
    function view_site_description(){
        echo get_bloginfo( 'description', 'display' );
        die();
    }
    add_action( 'wp_ajax_view_site_description', 'view_site_description' );
    add_action( 'wp_ajax_nopriv_view_site_description', 'view_site_description' );
    Thread Starter Bloke

    (@bloke)

    jQuery('#view_site_description').click(function(){
    Nothing happens. Looking in Firebug I don’t see any events triggered.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Can you link to a page with the issue?

    Thread Starter Bloke

    (@bloke)

    No its only local version. This is what I am doing. Only 3 functions. /wp/wp_ajax

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Maybe you’re not enqueing your JS properly and it’s being loaded before the DOM.

    Thread Starter Bloke

    (@bloke)

    Looking above is is included correctly? I am loading them in my theme’s functions file.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Looks like it, but you know we have to stab in the dark when we can’t see the site. What about wrapping a document.ready around your JS just in case?

    Thread Starter Bloke

    (@bloke)

    That is what I was thinking. In the exmaple they didn’t use that. How would that look?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    <script type="text/javascript">
    jQuery(document).ready(function() {
    
        jQuery('#view_site_description').click(function(){
            jQuery.ajax({
                type: 'POST',
                url: ajaxurl,
                data: {"action": "view_site_description"},
                success: function(data){alert(data);}
            });
            return false;
        });
    
    });
    </script>
    Thread Starter Bloke

    (@bloke)

    It worked! Thanks. But what should show in ‘display’?

    function view_site_description(){
        echo get_bloginfo( 'description', 'display' );
        die();
    }function view_site_description(){
        echo get_bloginfo( 'description', 'display' );
        die();
    }

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    ‘display’ doesn’t have to be passed, that’s a ‘filter’: http://codex.wordpress.org/Function_Reference/get_bloginfo

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘How to add ajaxurl’ is closed to new replies.