• Resolved dwaber

    (@dwaber)


    Hi,

    Your plugin is great, because it is simple. Thanks for that!

    Now, I’d like to enhance some functionality by using scripts for AJAX JQERY handling. How can I register this scripts for my hooks?

    I tried this, but had no susscess:

    add_action( 'init', 'my_script_enqueuer' );
    
    function my_script_enqueuer() {
       wp_register_script( "my_custom_script", WP_PLUGIN_URL.'/custom-css-js/my_custom_script.js', array('jquery') );
       wp_localize_script( 'my_custom_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        
    
       wp_enqueue_script( 'jquery' );
       wp_enqueue_script( 'my_custom_script' );
    
    }

    Can you give me some hints?

    Kind regards. Don

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Diana Burduja

    (@diana_burduja)

    Hello Don,

    the code you’re trying to add is a PHP code. The plugin can be used to add only CSS/JS/HTML codes to the website.

    You could use a plugin similar to Code Snippets to add your PHP code.

    • This reply was modified 4 years, 3 months ago by Diana Burduja.
    Plugin Author Diana Burduja

    (@diana_burduja)

    The custom codes are saved in the “/wp-content/uploads/custom-css-js/” folder, not in the “/wp-content/plugins/custom-css-js/” folder, as specified in your code.

    First you’ll need to retrieve the URL of the “uploads” folder and then use it to construct the URL to the custom code:

    add_action( 'init', 'my_script_enqueuer' );
    
    function my_script_enqueuer() {
       $dir = wp_upload_dir();
       wp_register_script( "my_custom_script", $dir['baseurl'] . '/custom-css-js/my_custom_script.js', array('jquery') );
       wp_localize_script( 'my_custom_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));        
    
       wp_enqueue_script( 'my_custom_script' );
    
    }
    Thread Starter dwaber

    (@dwaber)

    Thanks a lot for your help! Now I can finally use code snippet and your plugin together ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to register Script for ajax’ is closed to new replies.