• I have a problem with ajax and plugin in WordPress.
    In the plugin (frontend) I call ajax by clicking on a link and it works correctly. You can see this here https://goo.gl/RFda1O in the bottom page: if you click in menù “servizi offerti” ajax works and you can see “detail Service”.

    Now, in each row, that is “detail service”, I would add a link visible for only admin. Because I can remove, editing, add, the service.
    but, this second ajax in this link is not working!

    In the main file:

    `..

    function run_class() {

    $servizio = new diocesi_class();
    add_action(“init”,array ($servizio,”run”));
    add_action(“wp_footer”,array ($servizio,’load_script’));
    add_action( ‘wp_ajax_ajaxx’, array ($servizio, ‘prefix_ajaxx’ ));
    add_action( ‘wp_ajax_nopriv_ajaxx’, array ($servizio, ‘prefix_ajaxx’ ));

    //second call for ajax. Use this after the first call.

    add_action( ‘wp_ajax_ajaxx2’, array ($servizio, ‘prefix_ajaxx2’ ));
    add_action( ‘wp_ajax_nopriv_ajaxx2’, array ($servizio, ‘prefix_ajaxx2’ ));

    }

    run_class();

    ?>`

    In diocesi_class:

    ` class diocesi_class {

    public function __construct() {
    add_shortcode(“servizi”, array ($this,”run”));

    }

    public function run($atts, $content = null) {

    ….
    ….

    ob_start(); ?>
    <strong>Servizi offerti</strong><br/>
    <div id=”servizi_offerti”><div id=”menu_servizi”>
    <?php
    //create menu
    foreach ($servizi_disp as $servizi) {
    //menu for first call ajax and it’s working!
    echo “<a class=’ajaxLoad’ data-diocesi='”.$this->content.”‘ data-servizi='”.$servizi->id.”‘ href=’#’ >”.$servizi->nome.” </a> |”;

    } ?>
    </div>

    <p class=”info”>Per i dettagli selezionare dall’elenco il tipo di servizio</p>
    <div id=”informazioni_servizi”></div>
    <a href=”#” id=”ritorna” >Ritorna su <?php echo get_the_title(); ?></a>
    </div> <?php

    //For testing. It’s working, but not needed here!Look in the function prefix_ajaxx()
    if (current_user_can(‘edit_plugins’))
    {

    $link = plugin_dir_url(__FILE__) .’admin_servizi.php’;
    echo “<a class=’carica_admin’ data-diocesi='”.$this->content.”‘ data-servizi='”.$servizi->id.”‘ href=’#’ > Modifica Servizi </a>”;

    }

    return ob_get_clean();

    }

    }

    //register script
    public function load_script(){

    wp_enqueue_script( ‘jquery’ );

    wp_register_style(‘servizi’, plugin_dir_url(__FILE__) . ‘servizi.css’,array());
    wp_enqueue_style(‘servizi’);

    wp_register_script(‘ajaxx’, plugin_dir_url(__FILE__) . ‘ajax.js’,array(‘jquery’));
    wp_localize_script(‘ajaxx’, ‘ajax_params’, array(‘ajax_url’ => admin_url( ‘admin-ajax.php’ ) ));
    wp_enqueue_script(‘ajaxx’);

    //caricamento per admin
    // mostra la parte admin

    if (current_user_can(‘edit_plugins’)) {
    wp_register_script(‘ajaxx2’, plugin_dir_url(__FILE__) . ‘ajax2.js’,array(‘jquery’));
    wp_localize_script(‘ajaxx2’, ‘ajax_params’, array(‘ajax_url’ => admin_url( ‘admin-ajax.php’ ) ));
    wp_enqueue_script(‘ajaxx2’);

    }

    }

    //first ajax response
    public function prefix_ajaxx() {

    // return details services. Create table and rows.


    // ****************Event jquery NOT WORKING! ***********
    if (current_user_can(‘edit_plugins’))
    {

    echo “<a class=’carica_admin’ data-diocesi='”.$this->content.”‘ data-servizi='”.$servizi->id.”‘ href=’#’ > Modifica Servizi </a>”;

    }

    wp_die();
    }

    public function prefix_ajaxx2()
    {
    // response for second call ajax
    // edit row or single services (after click carica_admin)

    }
    else
    {
    echo “non si hanno i permessi!”;
    }
    wp_die();
    }`

    ajax2.js:

    `jQuery(“#ritorna”).hide();
    jQuery(“a.carica_admin”).click(function(){

    //var querystring = $(this).attr(‘href’);
    var diocesi = jQuery(this).data(‘diocesi’);
    var servizi = jQuery(this).data(‘servizi’);

    jQuery.ajax({

    url:ajax_params.ajax_url,
    type: “GET”,
    data:{
    action: ‘ajaxx2’,
    diocesi : diocesi,
    servizi : servizi
    },
    success: function(data) {
    jQuery(‘#menu_servizi’).hide();
    jQuery(‘p.info’).hide();
    jQuery(“#informazioni_servizi”).html(data);

    }
    });
    return false;
    });

    ajax.js

    jQuery(document).ready(function(){
    jQuery(“#ritorna”).hide();
    jQuery(“a.ajaxLoad”).click(function(){

    //var querystring = $(this).attr(‘href’);
    var diocesi = jQuery(this).data(‘diocesi’);
    var servizi = jQuery(this).data(‘servizi’);
    jQuery.ajax({

    url:ajax_params.ajax_url,
    type: “GET”,
    data:{
    action: ‘ajaxx’,
    diocesi : diocesi,
    servizi : servizi
    },
    success: function(data) {

    jQuery(“#informazioni_servizi”).html(data);
    jQuery(“#ritorna”).show();

    }
    });
    return false;
    });

    jQuery(“#ritorna”).click(function() {
    jQuery(“#informazioni_servizi”).empty();
    jQuery(“#ritorna”).hide();

    });

    });`

    Important! the problem is only out of public function run, for example in prefix_ajax where event jQuery click not work!

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp plugin event onclick not working in ajax response’ is closed to new replies.