Using require_once() in functions.php ONLY when WooCommerce plugin is activated - php

I'm using the Genesis Framework/WordPress. For theme development, I'm keeping my functions organized by fragmenting similar functions in my theme's /lib/ folder as includes and then using require_once() to include those functions in the appropriate order in functions.php. I'm aware of the performance considerations for this approach for highly dynamic sites and want to note this is for prod and staging only.
I have a collection of Woocommerce functions that I only want loaded when the WooCommerce plugin is activated. Below you can see the conditional statement that I've tried to construct in functions.php that would force wordpress to load this only when the plugin is activate. I'm admittedly new to PHP development and am having trouble discerning whether this is a method issue, syntax issue or both:
if ( ! function_exists( 'is_woocommerce_activated' ) ) {
function is_woocommerce_activated() {
if ( class_exists( 'woocommerce' ) ) {
require_once( CHILD_DIR . '/lib/cg-woocommerce.php' );}
else return; }
}
The stylesheets that I have enqueued in my woocommerce.php functions include are still being output in WP_head, which I'm assuming means that the conditional isn't working. Any ideas here or direction on how to troubleshoot?

Related

How to remove the woocommerce.css file from the theme?

I am using WordPress and I installed the Woocommerce plugin.
I added the template folder in my theme and started to customize my theme.
Now, I have to remove the Woocommerce.css from my theme and I found code on the official website here
I added the same code in the function.php
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
or
add_filter( 'woocommerce_enqueue_styles', '__return_false' );
but both answers are not working. I have installed the 5.7.1 Woocommerce.
How can I solve this issue?
function.php
function customtheme_scripts() {
wp_enqueue_style( 'customtheme-style', get_stylesheet_uri(), array(), time() );
wp_style_add_data( 'customtheme-style', 'rtl', 'replace' );
wp_enqueue_script( 'customtheme-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
wp_dequeue_style( 'customtheme-woocommerce-style' );
}
add_action( 'wp_enqueue_scripts', 'customtheme_scripts' );
view source
The domain name is just an example.
This answer has been fully tested on woocommerce 5.x+ and works fine on the default woo style sheets! If you're using a custom theme and/or custom style sheet then you may encounter some discrepancies.
What you see on the documentation page, no longer works on woo 4+, according to their github page.
So you need to dequeue its styles!
wp_dequeue_styleDocs
So if you only want to remove woocommerce.css file, then you could do this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('woocommerce-general'); // This is "woocommerce.css" file
}
However, if you want to remove all of the style sheets loaded by woo, then you could use this:
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('wc-block-vendors-style');
wp_dequeue_style('wc-block-style');
wp_dequeue_style('woocommerce-general');
wp_dequeue_style('woocommerce-layout');
wp_dequeue_style('woocommerce-smallscreen');
}
If you still can see the styles, then try to clean your cache.
UPDATE related to the question "custom style sheet"
At the time that I was writing the answer you had not provided any screenshot of your style sheet, nor did you say anything about using a custom style sheet. That's why you were not able to get it to work.
Please do NOT copy/paste if you're using a custom style sheet, like the custom css file used in the question. wp_dequeue_style function takes your style sheet handle as the argument. So please read the documentation first. You're using a custom handle name (i.e "customtheme-woocommerce-style"), therefore, you need to use that handle name.
add_action('wp_enqueue_scripts', 'removing_woo_styles');
function removing_woo_styles()
{
wp_dequeue_style('customtheme-woocommerce-style'); // This is your "custom style sheet" file.
}
Also note that commenting out the enqueue section in the main file (i.e inc/woocommerce.php) may work temporarily but on the next woo update, it'll come back again. So, it's recommended to avoid updating your template files as much as possible unless you really have to which is not the case here!

Call function from main WordPress theme in a plugin

I have a function in my theme functions.php file which returns a value:
function my_theme_function() {
return "100";
}
Anywhere in my theme templates I can simply do this...
echo my_theme_function()
...and I see the number 100 on the page. That's cool.
But in my plugin I would have expected to be able do also get access to this function by echoing my_theme_function() but instead I get a 'call to undefined function' error.
The strangest part is I'm certain this was working a couple of days ago, but I've not touched the code since. I suspect some WordPress shenanigans, but I don't know why or how to get around this.
The reason you may take this result can be the order in which the theme and the plugins are loaded.
For example, your plugin can get loaded before the theme, and obviously, in this case, the function it is not available in your plugin source code.
The solution to this issue are the WordPress Hooks. I don't know what is your plugin code style, but you can bootstrap your plugin in the init hook or even better in the after_setup_theme.
So for example, let's say, you need your plugin should run once your theme is loaded by the WordPress. You can use the following code to do so:
function my_theme_is_loaded() {
// Bootstrap your plugin here
// OR
// try to run your function this way:
if ( function_exists( 'my_theme_function' ) ) {
my_theme_function();
}
}
// You can also try replace the `after_setup_theme` with the
// `init`. I guess it could work in both ways, but whilw your
// plugin rely on the theme code, the following is best option.
add_action( 'after_setup_theme', 'my_theme_is_loaded' );
What the above code does, is like you say to your plugin, wait until the theme is totally loaded, and then try to run my plugin code that rely on the theme code.
And of course, I suggest either wrap your theme function in a plugin function like that:
// This way, your plugin will continue running even if you remove
// your theme, or by mistake your rename the function in the theme
// or even if you totally decide to remove the function at all in the
// side of the theme.
function function_from_theme() {
if ( function_exists( 'my_theme_function' ) ) {
return my_theme_function();
} else {
return 0; // Or a value that is suitable with what you need in your plugin.
}
}
This is going to protect your site against theme de-activation or theme change. In this cases, you are going to have a plugin looking for a function in your theme, and while you change the theme or deactivate your theme, your plugin will break your site.

Adding AMP pages to Wordpress manually without a plugin

I need to code my AMP Pages manually, and add them to my Wordpress site so I can make sure they are perfect. All the plugins I've used have not done everything I need, and cause errors in Search Console.
I've already created a child theme to play around in, and have been attempting to add a new PHP page template, but no luck!
The reason I'm coding manually is to
add proper structured data
amp-analytics code and
make sure everything will be indexed properly.
Have you tried this?
define( 'AMP_QUERY_VAR', apply_filters( 'amp_query_var', 'amp' ) );
add_rewrite_endpoint( AMP_QUERY_VAR, EP_PERMALINK );
add_filter( 'template_include', 'amp_page_template', 99 );
function amp_page_template( $template ) {
if( get_query_var( AMP_QUERY_VAR, false ) !== false ) {
if ( is_single() ) {
$template = get_template_directory() . '/amp-single.php';
}
}
return $template;
}
Source link.
We just created a AMP Template page outside of the WordPress environment. Programmed it with PHP/HTML. This makes the Fastest, Google Verified AMP page. Then just link to it from inside WordPress.
Note: We created sub-directories outside of WP to hold the AMP pages for better design control. Just watch out for the "Slug vs. sub-directory" name conflicts that will arise.
We also looked at all the AMP plugin, they are garbage as of 4/1/2017. They have to high of a code overhead to be SEO competitive. That overhead makes the page slower and a now WP AMP concept.

Wordpress Woocommerce cart scripts enqeue for shortcodes

I'm trying to figure out how to load the cart.min.js scripts not only if IS_CART but also if SHORTCODE_EXISTS('woocommrece_cart'), I can see that all the fronted scripts enqueues are in class-wc-frontend-scripts.php file and I don't want to override it, is there any hook that I can use to add another IF statement to the same file?
THANKS :)
after some testing
I want to explain my problem again:
I have a 'woocommerce_cart' shortcode that doesn't loads the scripts that it needs to work via ajax (like when updating cart and etc...) on other pages than the cart page
I see that in the class-wc-frontend-scripts.php the enqueue of that script goes only when is_cart() statement.
I am figuring out that I need to add if(shortcode_exists('woccommerce_cart')) so it would enqueue also when it is a shortcode.
I am adding that IF and I see that everything works on front end.
I am thinking that it is not a good idea to put that in the original plugin file because it would be overwritten with the next update.
I am trying to enqueue the script in my functions.php file with the new IF.
Nothing works....
Going back to the original file
On line 247 (here: https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-frontend-scripts.php) there is a function that adds more functionality to the ajax that without it it wont work...
Now I am wondering is there a way to add another IF to the same file using a hook? Or should I override all the file? (as I said before I think it is not a good practice because all the functions there wont get any updates).
So basically I know what I need to do, I just don't know what is the best way to do it and if it possible doing it with a hook or other function that maybe somebody knows and I'm not familiar with....
Thank you again for your help! :)
SHORT ANSWER
You need to define the WOOCOMMERCE_CART constant and make sure you're using a modern version of Woocommerce. I've tested this solution in Woocomerce 3.0.7.
if (!defined('WOOCOMMERCE_CART')) define( 'WOOCOMMERCE_CART', TRUE );
LONG ANSWER
I was having the same problem:
- I was calling the [woocommerce_cart] shortcode with the do_shortcode() function to display the cart in all the pages of my site.
- The cart was displaying well but the scripts (specifically cart.min.js) weren't loading in the pages. The effect of this is that the AJAX interactions weren't working.
The pages in my site were already running WC_Frontend_Scripts::init(). This function calls WC_Frontend_Scripts::load_scripts and that function loads our cart.min.js only if is_cart() (file wc-conditional-functions.php) is TRUE, as you already pointed.
Now, the current version of is_cart() evaluates TRUE when any of the following conditions is TRUE:
is_page( wc_get_page_id( 'cart' ) )
defined( 'WOOCOMMERCE_CART' )
wc_post_content_has_shortcode( 'woocommerce_cart' )
So, for me, the easiest way to fix the problem was to define the WOOCOMMERCE_CART constant.
Hope this helps. :-)
I had the exact same problem, combining cart and checkout pages broke the ajax auto cart
updates. I add the cart to the checkout page using:
function cart_on_checkout_page()
{
if ( is_wc_endpoint_url( 'order-received' ) ) return;
echo do_shortcode('[woocommerce_cart]');
}
add_action( 'woocommerce_before_checkout_form', 'cart_on_checkout_page', 5 );
My solution was to use the following code in functions.php to check if we are on the checkout page, and if so, enqueue/load the 'wc-cart' script. It's working with Woocommerce version 4.5.2.
function adjust_woocommerce_scripts()
{
if ( is_checkout() ) wp_enqueue_script( 'wc-cart' );
}
add_action( 'wp_enqueue_scripts', 'adjust_woocommerce_scripts', 11 );
You can adjust this function to perform whatever checks you need, such as if SHORTCODE_EXISTS('woocommrece_cart') - this means that the 'wc-cart' script is only loaded on the pages you need.

Wordpress plugin - how to disable js/css per page

With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.
I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.
<?php if ( is_page( '3486' ) ) { exit; } ?>
exit on a line by itself kills the page indicating that the script is being called.
The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?
I could name the plugin but the question is really more generic to any plugin.
You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript()
{
if ( is_page('YOUR PAGE NAME') )
{
wp_deregister_script( 'WORDPRESS JS file NAME' );
}
}
Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script

Categories