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' );
}