• I’m using a third-party plugin that uses a shortcode containing specific parameters.
    I’d like to create a button that changes those parameters to get the plugin to display different information.

    I wrote a client-side javascript that would replace the shortcode text on refresh before realizing shortcodes are handled server-side.

    So how would I do this? Enqueue the javascript? Trun it into an AJAX request somehow?

    I’ve done very little developing in wordpress and little-to-no ajax, so sorry if there is an obvious answer here.

    Thanks! ~gyz

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you want your button on the admin side of things, you can use your javascript in the editor to change the actual shortcode.

    But if you want your button to be on the front end of your site, you would need to use AJAX to get the rendered shortcode from the server and then display it where it goes. Or just make it a normal form submit that you handle in PHP as a different type of “shortcode” (you call the shortcode function directly). Both of these solutions need a little PHP, and only the AJAX one needs some javascript.

    Thread Starter gyzhor

    (@gyzhor)

    Cool. Thanks, Joy. I’m looking into Ajax options now.

    Thread Starter gyzhor

    (@gyzhor)

    Okay, I’ve been poking about with AJAX, but have hit a snag with admin-ajax.php.
    Whenever I send the call to admin-ajax.php I get a 400 error. I’ve tried calling to a test API online instead and the call went through.

    This is in the HEAD of my html:

    function swapVidGallery(newVidParam){
        jQuery.get('/wp-admin/admin-ajax.php', {'action':'newVidGallery'}, function(response){
    		console.log(response)
    		jQuery('#vidGalleryContent').html(response);
    	});
    }

    Could there be something wrong with my admin-ajax.php? Does it need to be enqueued or initialized in any way? Could it have been corrupted, or might I just not have permissions?

    Incidentally, the rest of my code is as follow, completely untested, of course, becasue I can’t get through to admin-ajax:

    FUNCTIONS.HTML:

    add_action('wp_ajax_newVidGallery', 'newVidGallery', 10, 1);
    add_action('wp_ajax_nopriv_newVidGallery', 'newVidGallery', 10, 1);
    
    function newVidGallery(newVidParam){
    	console.log("newVidGallery has been called:"+newVidParam)
    	if(newVidParam="UCIpxzcl4b3ggMODF6QnTOhQ"){
    		do_shortcode(vidShortcode+"searchResultsRestrictedToUser='"+newVidParam+"']");
    	} else {
    		do_shortcode(vidShortcode+"playlistValue='"+newVidParam+"']");
    	}
    	die();
    } 

    HTML BODY:

    <a href="swapVidGallery('UCIpxzcl4b3ggMODF6QnTOhQ')"> All Videos </a><br />
    <a href="swapVidGallery('PLMffcFdm_7-Hi6NibMNpEuc8rlqbBNxC8')"> Did You Know? </a><br />
    <a href="swapVidGallery('PLMffcFdm_7-HKkGlaF4AUByzzgnA_jsoD')"> Freezerworks 2017 & 2018 </a><br />
    <a href="swapVidGallery('PLMffcFdm_7-HQF17XpQ-fb3lYWXlYHKDR')"> Base Edition </a><br />
    • This reply was modified 6 years, 8 months ago by gyzhor.
    Thread Starter gyzhor

    (@gyzhor)

    Okay, it looks like my on-page script wasn’t sending to the ajax php due to an unexpected slash in the php url.
    So that’s working now. I can send to the php.

    NOW I’m having trouble in my functions.php script.
    For some reason, the code below isn’t being saved because of some error I can’t find (the php doesn’t accept pretty much any of this code, even when the rest is commented out).
    And when it doesn’t save, the response that gets returned is my entire index page.

    add_action('wp_ajax_newVidGallery', 'newVidGallery', 10, 1);
    add_action('wp_ajax_nopriv_newVidGallery', 'newVidGallery', 10, 1);
    
    function newVidGallery(newVidParam){
    	console.log("newVidGallery has been called:"+newVidParam)
    	if(newVidParam="UCIpxzcl4b3ggMODF6QnTOhQ"){
    		do_shortcode(vidShortcode+"searchResultsRestrictedToUser='"+newVidParam+"']");
    	} else {
    		do_shortcode(vidShortcode+"playlistValue='"+newVidParam+"']");
    	}
    	die();
    }  

    I’m baffled. Li’l help?
    ~gyz

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Swapping out plugin shortcode’ is closed to new replies.