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, 7 months ago by nobody123.
-
This reply was modified 2 years, 7 months ago by nobody123.
-
This reply was modified 2 years, 7 months ago by nobody123.
-
This reply was modified 2 years, 7 months ago by nobody123.
-
This reply was modified 2 years, 7 months ago by nobody123.
-
This reply was modified 2 years, 7 months ago by nobody123.
-
This reply was modified 2 years, 7 months ago by nobody123.
-
This reply was modified 2 years, 7 months ago by nobody123.