AJAX not Working with php as supposed

I am creating a like system in PHP . My code looks like this . <?php class Like_System { private $userid; private $postid; private $user_ko_like_count; private $post_ko_like_count; private $user_ko_dislike_count; private $post_ko_dislike_count; private $user_ip; public function __construct(){ } public function our_ajax_script(){ wp_enqueue_script( 'sb_like_post', get_template_directory_uri().'/data/js/post-like.min.js', false, '1.0', 1 ); wp_localize_script( 'sb_like_post', 'ajax_var', array( 'url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'like_system' ) ) ); } public function load_our_ajax_script(){ add_action( 'init', array($this,"our_ajax_script") ); add_action( 'wp_ajax_nopriv_like_system',array($this,"like_dislike_kernal")); add_action( 'wp_ajax_like_system',array($this,"like_dislike_kernal")); } public function verify_nonce($nonce){ if …
Category: Web

Front end post delete error confirmation and success message

I am allowing Authors to delete their posts on the front end as they cant access the admin. I have this working, but what bothers me is the standard javascript onclick alert message - is there are way to style a js alert? From my understanding, no. Or to use jQuery for the message and use a modal window that I can style? Also, after deletion the user is returned to the same page which is good, but there is …
Category: Web

How do I add <div> tags to entire comments, not just their text

Thanks to some wonderful help here at stack exchange, I have built a comment section with a dropdown menu that adds comment_meta to the comments. I currently have a working filter that uses GET to trigger an if statement to query based on this comment_meta info. The thing I don't like is that it refreshes the page each time you click a button. I'm looking for a way to filter without having the page refresh. I think I need Javascript …
Category: Web

How to Use JSON With AJAX?

I've defined a PHP callback function to handle the communication between server-side and client-side: function json_render_modal_body() { check_ajax_referer( THEME_PREFIX . '-modals', 'nonce' ); $response = array( 'text' =&gt; 'no' ); $modal_id = null; if ( isset( $_GET['modal_id'] ) &amp;&amp; ! $modal_id = filter_var( $_GET['modal_id'], FILTER_SANITIZE_STRING ) ) { wp_send_json_error( $response ); } if ( ! $form = render_form( $modal_id ) ) { wp_send_json_error( $response ); } $response['text'] = 'yes'; wp_send_json_success( $response ); } I've told WordPress about this function, to …
Category: Web

Trigger wp-embed via JavaScript to refresh iframe preview?

I'm running WordPress 6.0 and using the Block Editor to enable content editors to paste links that generate the embed/iframe preview, as described here. I've noticed from time to time (i.e. erratically) the embed script doesn't seem to run, and the link remains as static text without converting into the iframe preview. I'm also using Swup.js to handle page transitions throughout the site, and I'm wondering if wp-embed has a method of some kind that I can hook into to …
Category: Web

How to use wp_send_json() and output the value with ajax?

Hello Wordpress Developers. I create my own plugins, and I'm stuck about how to call php functions inside class, into javascript ajax functions. For example we have button with ID name clickme: &lt;button id=&quot;clickMe&quot;&gt;Click Me&lt;/button&gt; And then I make a class with functions, example like this: &lt;?php class myFunctions{ function a(){ $test = &quot;Hello World from wp_send_json()&quot;; wp_send_json($test); } } ?&gt; and then I want to output the test variable, using ajax. Example like this: import $ from &quot;jquery&quot;; class …
Category: Web

Create a Dropdown Selector and Redirect for a Custom Taxonomy in WordPress?

I'm trying to create a dropdown selector on my CPT and CT archive page for a custom taxonomy called &quot;location&quot;. I've managed to get the form to submit, however, it does not direct me to the archive page, rather it directs me to a URL that looks something like this https://example.com/?location=11 I'm wanting the destination URL to be this: https://example.com/location/term-name Bare with me as I have no background in Computer Science / IT so I'm learning on my own. Below …
Category: Web

enqueuing external and internal js and css in wordpress did not work with owl.js animate.css

&lt;?php function cadentic_files () { wp_enqueue_script('cadentic_owl_carousel',get_theme_file_uri('js/owl.carousel.min.js'), array('jquery'), time(), true ); wp_enqueue_script('cadentic_wow_carousel',get_theme_file_uri('js/wow.min.js'), array('jquery'), time(), true ); wp_enqueue_script('cadentic_jquery_carousel',get_theme_file_uri('js/jquery-multitabs.js'), array('jquery'), time(), true ); wp_enqueue_script('cadentic_jquery1113_carousel',get_theme_file_uri('js/jquery-1.11.3.min.js'), array('jquery'), time(), true ); //wp_enqueue_script('cadentic_bootstrap_carousel',get_theme_file_uri('js/bootstrap.min.js'), array('jquery'), time(), true ); wp_enqueue_style('cadentic_font_awesome','https://fonts.googleapis.com/css?family=Roboto:300,400,500,600,700,900', false); wp_enqueue_style('cadentic_font_popins','https://fonts.googleapis.com/css?family=Poppins:100,200,400,300,500,600,700', false); wp_enqueue_style('cadentic_font_confortaa','https://fonts.googleapis.com/css?family=Comfortaa:300,400,700|Josefin+Sans:100i,300,400,700|Raleway:100,200,300,400,500,600,700,800,900', false); wp_enqueue_style('cadentic_main_styles',get_stylesheet_uri(), false); wp_enqueue_style('cadentic_animate_styles',get_theme_file_uri('css/animate.css'), false); wp_enqueue_style('cadentic_owl_styles',get_theme_file_uri('css/owl.theme.default.min.css'), false); //wp_enqueue_style('cadentic_owlcarousel_styles',get_theme_file_uri('css/owl.carousel.min.css'), false); wp_enqueue_style('cadentic_bootstrap_theme_styles',get_theme_file_uri('css/bootstrap-theme.min.css'), false); wp_enqueue_style('cadentic_jquery_styles',get_theme_file_uri('css/jquery-multitabs.css'), false); wp_enqueue_style('cadentic_font_awesome_styles',get_theme_file_uri('css/font-awesome.min.css'), false); wp_enqueue_style('cadentic_main_css_styles',get_theme_file_uri('css/main.css'), false); wp_enqueue_style('cadentic_responsive_css_styles',get_theme_file_uri('responsive.css'), false); wp_enqueue_style( 'bootstrap', '//stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css', false); wp_enqueue_script( 'owl-js', 'https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js',array('jquery'), time(), true ); wp_enqueue_script( 'jq-js', 'https://code.jquery.com/jquery-3.5.0.js', array('jquery'), time(), true ); wp_enqueue_style( 'wpdocs-datatables-bootstrap-style', 'https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/dataTables.bootstrap4.min.css' ); wp_enqueue_style( 'wpdocs-bootstrap-style', 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css' ); // …
Category: Web

Show only images and videos in a wp.media window

I'm using the following JS code to open a wp.media window to allow users to select images and videos for a gallery. Everything is working as expected, but I'm unable to restrict the window to show images and videos only, it's showing everything. Any ideas on what might be wrong? Thanks in advance JS: $( '#add_item' ).on( 'click', function( e ) { var $el = $( this ); e.preventDefault(); // If the media frame already exists, reopen it. if ( …
Category: Web

Draggable item in custom gutenberg block

I'm developing some custom blocks in the new Gutenberg editor experience, and I'm struggle to understand how to use some build-in components, mostly the Draggable components. What I would like to achieve is a list of items (let's say many li in a ul) and I want them to be orderable with a drag &amp; drop feature. Here is my code: import { __ } from '@wordpress/i18n'; import { registerBlockType } from '@wordpress/blocks'; import { Draggable, Dashicon } from '@wordpress/components'; …
Category: Web

Translate javascript with WordPress built-in localization API for static strings

I have been trying to implement the translation to a couple of strings in a .js file but nothing has worked. First try to do it like I do in php, in the .js file add this variable var __ = wp.i18n.__; and I put the strings like __( 'Hello World', 'text-domain'), but it didn't work. After looking for information about it, I found a post where they talk about passing the translated strings with wp_localize_script something like that. // …
Category: Web

Set Button in PluginDocumentSettingPanel Content (Wordpress Gutenberg)

I'm trying to create a new panel in the edit-post sidebar (Wordpress Gutenberg), I already managed to set the panel with the title and the text in the content part but I need to add a button there and no I find a way to do it. var registerPlugin = wp.plugins.registerPlugin; var PluginDocumentSettingPanel = wp.editPost.PluginDocumentSettingPanel; // var btn = wp.element.createElement( 'button' ); var el = wp.element.createElement; var __ = wp.i18n.__; /* I tried to do it with a function, but …
Category: Web

Modifying child theme's header

I'm a noob. I have copied the header.php from the parent theme, I have added some snippets and things work. However, I would like to be able to add those same snippets either using a plugin or via functions.php so when/if the theme is updated, I don't need to redo everything. I've tried many things but none of them work. I wonder if the problem is where the snippets are placed by using a plugin or that the HTML element …
Category: Web

Hook on file upload

I need a hook or some way to detect when the window to select the file to upload is opened. I'm talking about the window that opens the browser to select the local files to upload. Context: On occasions, WordPress itself or some plugins directly open a window to select the local file to upload, without going through the WordPress library. I have a plugin that gives a custom path when uploading files and I need to launch a popup …
Category: Web

`wp_set_script_translations` with `wp.i18n` does not return translated strings in simple plugin

Short description: I try to translate strings in JS file. To test it I decided to create a simple test plugin. I have PHP and JS strings there. Translated PHP strings work fine, JS strings don't work. Tools and environment: WP-CLI 2.4.0 Wordpress 5.5.1 Ubuntu 20.04 Original languange: English (United States) Translate language: German Plugin PHP file content: &lt;?php /* * Plugin Name: Test * Text Domain: test * Domain Path: /languages */ /** * Init all */ function run() …
Category: Web

Rhyzz Repeatable-fields javascript in php and wordpress

I'm having a hard time figuring out why my repeatable fields javascript doesn't work. I'm using the following plugin: Rhyzz (repeatable-fields): http://www.rhyzz.com/repeatable-fields.html. I scraped the website and I see he ran the script inside html and also ran his javascript all on the same page. My website is a lot larger. I'm using Wordpress and I did the following to call the script in functions.php. wp_enqueue_script( 'jquery', 'https://code.jquery.com/jquery-1.11.2.min.js', array(), '20120206', true ); wp_enqueue_script( 'jquery-ui', 'https://code.jquery.com/ui/1.11.4/jquery-ui.min.js', array(), '20120206', true ); wp_enqueue_script( …
Category: Web

How do I output user_registered time in my correct timezone?

I am currently using Wordpress, and an element of user_registered returns the time of registration of the user. I have set the correct timezone, but don't know how to get the user_registered output to match my current time. So far, this is what I have. date_default_timezone_set('America/Los_Angeles'); $users = get_users(); foreach( $users as $user ) { $udata = get_userdata( $user-&gt;ID ); $registered = $udata-&gt;user_registered; printf( '%s member since %s&lt;br&gt;', $udata-&gt;data-&gt;display_name, date( "Y-m-d H:i:s", strtotime( $registered ) ) ); } echo('date below …
Category: Web

include javascript Multiple files in wordpress through script.js

i have script.js file for html to wordpress conversion. In html it works, but in wordpress it does not work. This file contains slider, menus etc, I enable to convert html to wordpress without this file, the file has following contents function include(scriptUrl) { document.write('&lt;script src="' + scriptUrl + '"&gt;&lt;/script&gt;'); } function isIE() { var myNav = navigator.userAgent.toLowerCase(); return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false; }; /* cookie.JS ========================================================*/ include('js/jquery.cookie.js'); /* Easing library ========================================================*/ include('js/jquery.easing.1.3.js'); /* Stick up menus …
Category: Web

Gutenberg add extra attributes to custom format

I'm creating a custom format (adding a custom option to the format toolbar) using the following guide: https://developer.wordpress.org/block-editor/how-to-guides/format-api/ import { registerFormatType, toggleFormat } from '@wordpress/rich-text'; import { RichTextToolbarButton } from '@wordpress/block-editor'; const MyCustomButton = ( { isActive, onChange, value } ) =&gt; { return ( &lt;RichTextToolbarButton icon=&quot;editor-code&quot; title=&quot;Sample output&quot; onClick={ () =&gt; { onChange( toggleFormat( value, { type: 'my-custom-format/sample-output', } ) ); } } isActive={ isActive } /&gt; ); }; registerFormatType( 'my-custom-format/sample-output', { title: 'Sample output', tagName: 'samp', className: null, …
Category: Web

Ajax call from Plugin using Class

I'm making a plugin with a class and methods. This class gets called through the theme ( hard coded ) to enqueue and localize my js. it also prepares it for ajax. However, i keep getting ERROR: 400 POST http://www.devsite.local/wp-admin/admin-ajax.php 400 (Bad Request) I've ran ajax outside of a class in multiple plugins and at the functions.php with no issues, but i can't get it to work inside a class. This is my code: class CCYTFeatured { public function __construct(){ …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.