Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    No. What are you expecting to see? Can you post some screen shots since we cannot see pages hosted locally?

    Thread Starter php-coder

    (@php-coder)

    I am expecting like this:
    <script type='text/javascript' src='https://localhost/wordpress/wp-includes/js/jquery/jquery.min.js?ver=3.5.1' id='jquery-core-js'></script>

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    WordPress does not include jQuery on the front end of the site by default. Many themes include jQuery there, but it’s not there automatically.

    Thread Starter php-coder

    (@php-coder)

    When I browse https://localhost/wordpress > “View Page Source” I see jquery there. Why is this?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Your theme or a plugin may be enqueueing jQuery for your home page.

    Thread Starter php-coder

    (@php-coder)

    Theme codes:

    //header.php
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
        <head>
            <?php wp_head(); ?>
        </head>
    
        <body>
    
    //index.php
    <?php
    	get_header();
    
        while ( have_posts() ) : the_post();
            the_title();
            the_content();
        
        endwhile;
        wp_reset_query();
    
    	get_footer();
    ?>
    
    //footer.php
        <?php wp_footer(); ?>
        </body>
    
    </html>
    
    //style.css
    /*
    Theme Name: Custom Test
    Description: To test
    Version: 1.0
    */

    Plugin codes:

    <?php
    /*
    Plugin Name: Form Ajax
    Plugin URI: https://
    Description: Form ajax request
    Version: 1.0
    Author: User1
    License: GPLv2 or later
    Text Domain: form-ajax
    */
    
    if (!defined('ABSPATH')) { 
        exit;
    }
    
    function form_ajax_add_theme_codes() {
        
    	// Enqueue javascript on the frontend.
        
        wp_enqueue_script( 'example-ajax-script', plugins_url( 'js/simple-ajax-example.js', __FILE__ ), array('jquery'), null, true );
        /*
        if( ! wp_script_is( 'jquery', 'enqueued' ) ) {
            echo 'no jquery';
            exit();
        }
        */
    	// The wp_localize_script allows us to output the ajax_url path for our script to use.
    	wp_localize_script(
    		'example-ajax-script',
    		'example_ajax_obj',
    		array(
    			'ajaxurl' => admin_url( 'admin-ajax.php' ),
    			'nonce' => wp_create_nonce('ajax-nonce')
    		)
    	);
    
    }   
    add_action('wp_enqueue_scripts', 'form_ajax_add_theme_codes');
    
    function example_ajax_request() {
     
        $nonce = $_POST['nonce'];
        //print_r($nonce);
        //print_r($_POST);
        //print_r($_REQUEST);
        //exit;
        if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) ) {
            die( 'Nonce value cannot be verified.' );
        }
     
        // The $_REQUEST contains all the data sent via ajax
        if ( isset($_REQUEST) ) {
         
            $fruit = $_REQUEST['fruit'];
             
            // Let's take the data that was sent and do something with it
            if ( $fruit == 'Banana' ) {
                $fruit = 'Apple';
            }
         
            // Now we'll return it to the javascript function
            // Anything outputted will be returned in the response
            echo $fruit;
             
            // If you're debugging, it might be useful to see what was sent in the $_REQUEST
            // print_r($_REQUEST);
         
        }
         
        // Always die in functions echoing ajax content
       die();
    }
     
    add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
    add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
    
    function form_ajax_form() {
    ?>
        <form method="POST" action="" >
            <div><input type="text" name="username" placeholder="Username" /><div>
            <div><input type="button" value="Submit" /></div>
        </form>
        <div id="id_div_fruit"></div>
    <?php
    }
    function form_ajax_show_form() {
        add_shortcode( 'form_ajax_form', 'form_ajax_form' );
    }
    add_action('init', 'form_ajax_show_form');

    Javascript codes:

    //simple-ajax-example.js
    jQuery(document).ready(function($) {
     
        // We'll pass this variable to the PHP function example_ajax_request
        var fruit = 'Banana';
         
        // This does the ajax request
        //console.log($);
        $.ajax({
            type: 'POST',
            url: example_ajax_obj.ajaxurl,
            data: {
                'action': 'example_ajax_request',
                'fruit' : fruit,
                'nonce' : example_ajax_obj.nonce
            },
            success:function(data) {
                // This outputs the result of the ajax request
                console.log(data);
                $("#id_div_fruit").append(data);
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });  
                  
    });
    • This reply was modified 3 years, 8 months ago by php-coder.
    • This reply was modified 3 years, 8 months ago by php-coder.
    • This reply was modified 3 years, 8 months ago by php-coder.
    Thread Starter php-coder

    (@php-coder)

    @sterndata , I think, no code is adding jquery for home page.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    That line of code you posted in what is including jQuery:

    wp_enqueue_script( 'example-ajax-script', plugins_url( 'js/simple-ajax-example.js', __FILE__ ), array('jquery'), null, true );

    That “array(‘jquery’)” piece is the dependency. It says that the simple ajax example code needs jquery, so it includes it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Is jquery disabled for new pages?’ is closed to new replies.