Remove JS file from Woocommerce Product edit pages - wordpress

I am getting this error "Uncaught Error: Option 'ajax' is not allowed for Select2 when attached to a element." while updating Product Variation.
Actually there are 2 select2.js files, one from Woocommerce and other from 'WR PageBuilder' plugin. While I am renaming 'WR PageBuilder' select2.js file then its working fine. But that file is required for Editor.
I want to remove that js file only from Product pages.
I did 'wp_deregister_script()' and 'wp_dequeue_script()' but nothing happened.
Here is my code:
add_action('admin_init', 'functon_to_filter_script');
function functon_to_filter_script() {
global $typenow;
// when editing pages, $typenow isn't set until later!
if (empty($typenow)) {
// try to pick it up from the query string
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
}
if( 'product' == $typenow ){
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
}
}
function deregister_my_script() {
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
can anyone give me a solution?

This won't work because you are using the actions wrong.
Look here for the correct usage of action hooks:
Hooks in Wordpress
You are putting the admin_enqueue_scripts action hook inside of the admin_init action hook.
Try taking admin_enqueue_scripts outside of the admin_init hook like this:
global $typenow;
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
function deregister_my_script() {
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
if( 'product' == $typenow ){
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
}

Related

WordPress - woocommerce_process_shop_order_meta doesn´t work

I am trying to include a function after an order is updated (admin side), but it doesn´t work. I want to call a function when someone update an order (order status, etc...). I am trying this:
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 1, 1);
function woocommerce_process_shop_order () {
// Code
}
Ive been trying to redirect to other web with header("Location: www.example.com"), but the page doesn´t redirect when an order is update :(
Please, can you help me to solve it?
Thanks!
There you go. Tested and works. Goes into functions.php file of your child theme:
add_action( 'post_updated', 'post_updated_action', 20, 3 );
function post_updated_action( int $post_id, WP_Post $post_after, WP_Post $post_before ): void {
// To be sure it gets called when updated from the admin dashboard and is order
if ( $post_before->post_type !== 'shop_order' || ! is_admin() ) {
return;
}
wp_safe_redirect( home_url() );
exit;
}

Issue with output of WooCommerce hooks on cart and checkout page

I am trying to add some text below the applied coupon on my cart page, but for some reason, I can only get it to display above the table, as per the screenshot.
I even created a fresh install with the twentytwentyone theme, and no other plugins installed besides woocommerce.
These are the code that I am using:
add_action('woocommerce_cart_totals_before_shipping', 'bb_before_shipping');
function bb_before_shipping() {
echo 'woocommerce_cart_totals_before_shipping';
}
And
add_action('woocommerce_before_cart_totals', 'apply_product_on_coupon');
function apply_product_on_coupon() {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
echo 'woocommerce_before_cart_totals';
}
}
Any help will be appreciated!
You are using the correct hook for the cart page, but the output is part of a HTML table.
So you get:
// Cart
function action_woocommerce_cart_totals_before_shipping() {
echo '<tr><td>woocommerce_cart_totals_before_shipping</td></tr>';
}
add_action( 'woocommerce_cart_totals_before_shipping', 'action_woocommerce_cart_totals_before_shipping' );
// Checout
function action_woocommerce_review_order_before_shipping() {
echo '<tr><td>woocommerce_review_order_before_shipping</td></tr>';
}
add_action( 'woocommerce_review_order_before_shipping', 'action_woocommerce_review_order_before_shipping' );

How to retrieve query list of post_type in WordPress?

I am developing a plugin and need the list of post types existing in the WordPress backend, I checked the WP_Query page of WordPress codex but could not find the solution
You can try this code.
<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever. Any time after init is usually
fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init()
{
$types = get_post_types( [], 'objects' );
foreach ( $types as $type ) {
if ( isset( $type->rewrite->slug ) ) {
// you'll probably want to do something else.
echo $type->rewrite->slug;
}
}
}
i think it will work for you

How to add CSS only to the options page of a plugin in WordPress?

I want to add a stylesheet for the options_page of my plugin only. But how to do that? My code so far:
function add_options_page_style() {
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_menu', 'add_options_page_style' );
I could place an if statement before the line with add_action... but I'm not sure how to filter my options page. I already tried the $pagename variable and also this line: $wp_query->queried_object->post_name; but it didn't work.
The filter $_GET['page'] does work but might break in future versions.
Somewhere you'll be registering page like this:-
function register_page(){
global $page_hook_suffix;
$page_hook_suffix = add_options_page('Your_plugin', 'Your_plugin', 'manage_options', __FILE__, 'display_form');
}
add_action('admin_menu', 'register_page');
And while enqueueing script you'll do something like this:-
function my_enqueue($hook) {
global $page_hook_suffix;
if( $hook != $page_hook_suffix )
return;
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function load_custom_wp_admin_style($hook) {
// Load only on ?page=mypluginname
if($hook != 'toplevel_page_mypluginname') {
return;
}
wp_enqueue_style( 'custom_wp_admin_css', plugins_url('admin-style.css', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
add your page slug as suffix to toplevel_page_
e.g. if page slug is this-plugin-options
then
if($hook != 'toplevel_page_this-plugin-options') {
return;
}
here is wordpress doc with
Example: Load CSS File on All Admin Pages,
Example: Load CSS File from a plugin on specific Admin Page

wp_query is empty in admin_init

I'm developing a plugin and one of the issues I am running into is that I cannot get the post id within a function assigned to the admin_init hook.
I tried a few different methods; but, they all seem to use the $wp_query.
Below is a simple version the code I am using. I implemented the code like this just now and ran it by viewing the "post edit" page
add_action('admin_init','do_optional_featured_article');
function do_optional_featured_article()
{
global $wp_query;
echo "<pre>";
print_r($wp_query);
echo "</pre>";
die();
}
$wp_query is a mostly empty array, notably, the post member is empty
-- EDIT --
I got some advice over at wordpress.stackexchange and added in this function:
function get_admin_post()
{
if( isset($_GET['post']) )
{
$post_id = absint($_GET['post']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
elseif( isset($_POST['post_ID']) )
{
$post_id = absint($_POST['post_ID']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
else
{
return false;
}
}
I think this answer will help. It states that the earliest action you can hook into, to get the global $post/$posts variables is the wp action. In the action hook reference on the codex, you can see that the wp action executes a bit after admin_init, which is why you can't retrieve any posts there, I think.
So, that should work:
add_action('wp','do_optional_featured_article');

Resources