Forum Replies Created

Viewing 6 replies - 16 through 21 (of 21 total)
  • Thread Starter nobody123

    (@nobody123)

    @bcworkz Thanks you very much!
    It work when I use your code.
    but I still have some question want to ask
    1.foo : foobar should be change to foo : 'foobar'
    2.How do receive php echo using your code?
    I found adding any code in if ( wp_verify_nonce( $_POST['_wpnonce'], 'wp_rest' ) ), $ajax.do would be unsuccessful.

    Thread Starter nobody123

    (@nobody123)

    @bcworkz Thank you very much!
    1.what is foo : foobar?

    2.I changed your code from add_action('wp_enqueue_scripts', 'my_enqueue2'); to add_action('admin_enqueue_scripts', 'my_enqueue2');, and then the Console logs don’t display anything….
    Could you tell me where you put php and js file ?

    3.Due to modify plugin on background management sys., I have to work on admin_(the frontend of background management sys.) but not wp_ (the frontend of wp)

    4.if I use <form action='XXX.php'>, it will redirect to a new page, not in wordpress frame.

    5.Does admin-ajax.php and the hook of wp_ajax_ is ok or not on the frontend of background management sys.?

    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    Thread Starter nobody123

    (@nobody123)

    @bcworkz Cloud you provide your all of code working to me, Please
    I want to try does it work or not in plugin (admin).
    I found many people had similar problem(400 error) in stack overflow.
    does it be a bug for admin_ not wp_ ?
    Thank you very much

    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    Thread Starter nobody123

    (@nobody123)

    I have added the following code to my_plugin_name.php in static function enqueue()

    $variables = array(
     'ajaxurl' => admin_url( 'admin-ajax.php' )
    );
     wp_localize_script('mycrawlerscript', "test", $variables);

    and changed js from url: '/wordpress/wp-admin/admin-ajax.php' to url: test.ajaxurl or url: 'https://127.0.0.1:8080/wordpress/wp-admin/admin-ajax.php'
    both are get 400 error.

    • This reply was modified 2 years, 11 months ago by nobody123.
    Thread Starter nobody123

    (@nobody123)

    Hi @bcworkz ,
    I have changed my code to the following

    JS (app.js):

    jQuery(document).ready(function($) {
        // We'll pass this variable to the PHP function example_ajax_request
        $('.submit').click(function (){
            var my_data = {
                'action':'crawler_info',
                'my_info' : document.getElementById('url').value
            };
            console.log(my_data);
            // This does the ajax request
            $.ajax({
                url: '/wordpress/wp-admin/admin-ajax.php',
                data: my_data,
                dataType: 'json',
                success:function(data) {
                    // This outputs the result of the ajax request
                    console.log(data);
                },
                error: function(errorThrown){
                    console.log(errorThrown);
                }
            }); 
        })
    });

    PHP(index.php):

    <?php
    /**
     * 
     * @package PLUGIN_NAME
     */
    
    function get_info() {
    
        // The $_REQUEST contains all the data sent via ajax 
        if ( isset($_REQUEST) ) {
    
            $infos = json_encode($_REQUEST['my_info']);
    
            // Now we'll return it to the javascript function
            // Anything outputted will be returned in the response
            echo $infos;
    
            // 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 or it will display 0 or another word
       die();
    }
    
    add_action( 'wp_ajax_crawler_info', 'get_info' );
    add_action( 'wp_ajax_nopriv_crawler_info', 'get_info' );

    PHP2(my_plugin_name.php):

    <?php
    /**
     * Plugin Name:       Crawler Plugin
     * Plugin URI:        https://www.google.com/
     * Description:       Crawler for real estate Plugin.
     * Version:           1.0.0
     * Requires at least: 5.8
     * Requires PHP:      7.4
     * Author:            Ming
     * License:           GPL v2 or later
     * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
     * Update URI:        https://www.google.com/
     * Text Domain:       my-basics-plugin
     * Domain Path:       /languages
     * @package CRWLER
     */
    
    defined('ABSPATH') or die('Hello, i/m your world');
    
    if ( !class_exists('Crawler')){
    
        class Crawler
        {        
            function __construct()
            {
    			$this->plugin = plugin_basename( __FILE__ );
                $this->my_menu_url = 'my_crawler';
            }
    
           
            function register(){
                add_action('admin_enqueue_scripts', array( $this , 'enqueue') );
                add_action('admin_menu', array($this, 'add_admin_pages'));
               
                add_filter("plugin_action_links_$this->plugin", array( $this, 'setting_link'));
            }
    
            public function setting_link($links){
                $settings_link = '<a href="options-general.php?page='.$this->my_menu_url.'">Setting</a>';
                array_push($links, $settings_link);
                return $links;
            }
    
            public function add_admin_pages(){
                add_menu_page('Crawler Plugin', 'Crawler1', 'manage_options', $this->my_menu_url, array($this, 'admin_index'), 'dashicons-store', 110);
            }
            
            public function admin_index(){
                require_once plugin_dir_url(__FILE__) . 'src/admin.html';
            }
    
            protected function create_post_type(){
                
                add_action('init', array( $this, 'custom_post_type') );
            }
            
            function custom_post_type(){
                register_post_type('book', ['public' => true, 'label' => 'testtest']);
            }
    
            static function enqueue(){
                wp_deregister_script( 'jquery' );
    
                wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js');
                wp_register_script('bootstrap_js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js');
                wp_register_style('bootstrap_css','https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css');
                
                wp_enqueue_style('mycrawlerstyle', plugins_url('/crawler/assets/css/app.css'), __FILE__, null);
                wp_enqueue_style('bootstrap_css');
                wp_enqueue_script('mycrawlerscript', plugins_url('/crawler/assets/js/app.js'), __FILE__, null, true);
                wp_enqueue_script('jquery');
                wp_enqueue_script('bootstrap_js');
            }
    
            function activate(){
    			require_once plugin_dir_url(__FILE__) . 'src/activate.php';
            } 
        }
    
        $crawler = new Crawler();
        $crawler -> register();
    
        // activate
        register_deactivation_hook(__FILE__,  array( 'CrawlerActivate', 'activate') );
    
        // deactivate
        require_once plugin_dir_url(__FILE__) . 'src/deactivate.php';
        register_deactivation_hook(__FILE__,  array( 'CrawlerDeactivate', 'deactivate') );
    }

    =============================================

    my file directory:

    my_plugin_name—assets
    | |
    | —js
    | |
    | –app.js
    |
    —src
    | |
    | –admin.html
    |
    —index.php
    |
    —my_plugin_name.php

    Version:
    WordPress 5.8.2
    PHP 7.4.27
    MySQL 5.7.37 Community Server (GPL)

    Finally, I still got that error….

    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    Thread Starter nobody123

    (@nobody123)

    @bcworkz
    No matter how do I change my code adding dataType: 'json' in ajax and from $infos = $_POST['my_info']; to $infos = json_encode($_POST['my_info']); in php
    or
    adding dataType: 'html' in ajax
    both get the same “400 (Bad Request)” error.
    How do I change my code to let it work?
    Thanks

    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
    • This reply was modified 2 years, 11 months ago by nobody123.
Viewing 6 replies - 16 through 21 (of 21 total)