2

I've read probably more than 30-40 questions about it here but couldn't implement any of them. I say "couldn't" because probably I miss something or trying to make them work on wordpress have some difficulties.

  • I have a button on one of the pages I have. That button is created with Shortcodes Ultimate and it can have an onclick value.

[su_button url="#" style="flat" background="#ed2079" size="2" wide="yes" icon="icon: pencil" onclick=execJS();]Click Me![/su_button]

  • I have created a file named faucad.js and put it's reference to that page's head section. I can trigger that .js' function successfully by clicking that button.

Content of faucad.js

function execJS() {
   do_shortcode([my_shortcode cmd="take"]);

}

  • Problem is, whatever code I wrote there, couldn't trigger a shortcode I have. Shortcode is created with a snippet plugin and it's like that;

add_shortcode( 'my_shortcode', 'my_shortcode_array' );

It takes one attribute called "cmd" and it should be "take".

It doesn't recognize that function, can't build a bridge with wordpress;

do_shortcode([my_shortcode cmd="take"]);

How can I access that shortcode? All I want to achieve is to trigger a shortcode on a button click.

Please tell me to tailor my question if it's complicated.

Thanks,

UPDATE:

If I make dataType as "json" I get "Uncaught TypeError: Cannot read property 'type' of null" error. If I make it as "html", I always get "Failed" message and code doesn't touch do_shortcode() function.

JS

function gxfd_claim() {
    $(document).ready( function() {
        $.ajax({
            type : "post",
            dataType : "json",
            url : myAjax.ajaxurl,
            data : {action: "gxfd_claim_function"},
            success: function(response) {
                if(response.type == "success") {
                    alert("Success!")
                }
                else {
                    alert("Failure!")
                }
            }
                })
                    })
}

PLUGIN'S PHP

add_action('wp_ajax_nopriv_gxfd_claim_function', 'gxfd_claim_function');
add_action('wp_ajax_gxfd_claim_function', 'gxfd_claim_function');
function gxfd_claim_function() {
    do_shortcode('[gx_faucad cmd="claim"]');
    die();
}

add_action( 'init', 'my_script_enqueuer' );
function my_script_enqueuer() {
   wp_register_script( "faucad", WP_PLUGIN_URL.'/gx-internal/faucad.js', array('jquery') );
   wp_localize_script( 'faucad', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        

   wp_enqueue_script( 'jquery' );
   wp_enqueue_script( 'faucad' );
}
Puwhae
  • 67
  • 1
  • 7
  • 1
    *Calling a PHP function from Javascript...* the short answer is, you can't. PHP runs on the server and JavaScript the client machine... you can sort of work around it with Ajax. – CD001 May 08 '18 at 13:38
  • 3
    it's not possible to call PHP directly in Javascript. You could use an AJAX request to access a PHP function. There is a few question here about that [here is an example](https://stackoverflow.com/questions/43557755/how-to-call-ajax-in-wordpress). – Nicolas May 08 '18 at 13:38
  • I can't seem to make it work. I've added whole code content to my original post. Please check when you are able. – Puwhae May 08 '18 at 21:04
  • What version of jQuery are you using? – Tom Smilack May 08 '18 at 21:45

1 Answers1

1

Try changing

success: function(response) {
   if(response.type == "success") {
      alert("Success!")
   }
   else {
      alert("Failure!")
   }
}

to

success: function(response, status) {
   alert(status);
}

The success function is passed these arguments, according to http://api.jquery.com/jQuery.ajax:

(Anything data, String textStatus, jqXHR jqXHR)

What might be happening is that the PHP script returns no data, so the data (response) parameter is null, and it throws the TypeError when you try to access type from the null parameter.

Tom Smilack
  • 2,057
  • 15
  • 20
  • Thanks. Now I can get success message but still, it doesn't touch do_shortcode() function in that .php. Update: and jQuery I think is JQMigrate 1.4.1 as wordpress' default. – Puwhae May 08 '18 at 22:05
  • Unfortunately I don't have enough experience with Wordpress to debug that part – Tom Smilack May 10 '18 at 03:34