How to PREVENT a plugin to run on a SPECIFIC PAGE? - php

I am looking for a solution that will NOT activate a certain plugin on a SINGLE PAGE.
I have tried to play around with the attached code (offered by #Kamil Grzegorczyk on Wordpress Disable Plugin on Specific Pages/Posts) but with no success.
add_filter( 'option_active_plugins', 'lg_disable_cart66_plugin' );
function lg_disable_cart66_plugin($plugins){
if(strpos($_SERVER['REQUEST_URI'], '/store/') === FALSE AND strpos($_SERVER['REQUEST_URI'], '/wp-admin/') === FALSE) {
$key = array_search( 'cart66/cart66.php' , $plugins );
if ( false !== $key ) unset( $plugins[$key] );
}
return $plugins;
}
I have also tried to us plugin organizer but it is not helping to NOT activate a certain plugin on a SINGLE PAGE.
BTW, if anyone got a code that can only activate certain plugins such as Contact form no 7 by tracing their shortcode on a post/page that will be brilliant!
Something like if x page/post has a shortcode that contains 'contact form no 7' then activate Contact form no 7 plugin, else, do not activate.
Anyway, at this stage, an easy to go solution will do :)
Thanks.

"BTW, if anyone got a code that can only activate certain plugins such as Contact form no 7 by tracing their shortcode on a post/page that will be brilliant! Something like if x page/post has a shortcode that contains 'contact form no 7' then activate Contact form no 7 plugin, else, do not activate." - This is not possible. Reading the source code wp-blog-header.php should make this clear.
$wp_did_header = true;
// Load the WordPress library.
require_once( dirname( __FILE__ ) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
Plugins are loaded by wp-settings.php which is loaded by wp-load.php. The post content of a request is retrieved from the database by WP::query_posts() which is called by the function wp(). So, plugins are already loaded before the post content is even retrieved from the database.
Where did you put the code:
add_filter( 'option_active_plugins', 'lg_disable_cart66_plugin' );
This cannot be put in a regular plugin since it must execute BEFORE plugins are loaded. (You cannot tell a plugin not to load in code that is already loaded.) It also cannot be put in functions.php since functions.php is loaded AFTER plugins are loaded. Did you read the comment about mu-plugins? "must use" plugins are loaded before regular plugins. All of this can be found in wp-settings.php - I highly recommend reading it - it is an easy read and you will understand how WordPress initializes itself.

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!

Setting Wordpress front page as Algolia search

I've recently setup a new Wordpress install to act as a survey database. The purpose of the site is to collect survey data and allow the admin's to filter and search submitted survey data.
I've installed and configured the Algolia search WP plugin. Everything is working properly. If I navigate to 'mydomain.com/?s=' I see the search form and it's returning results.
My question is how can I set the Algolia search page as my Wordpress index/front page? Or, how can I import this form to a page that I can designate as my WP static front page?
Further info: I have a child theme installed and can create custom page templates/template-parts
The reason is this code here...
if ( is_search() && $settings->should_override_search_with_instantsearch() ) {
return $this->load_instantsearch_template();
}
from this file
https://github.com/algolia/algoliasearch-wordpress/blob/1a51d3e2e8be12bfcbccd1ef2a722911a99376e7/includes/class-algolia-template-loader.php
Essentially it isn't being loaded at present where you want.
Putting this code in your functions.php will fix it.
add_action( 'wp_enqueue_scripts', function () {
// Enqueue the instantsearch.js default styles.
wp_enqueue_style( 'algolia-instantsearch' );
// Ensure jQuery is loaded.
wp_enqueue_script( 'jquery' );
// Enqueue the instantsearch.js library.
wp_enqueue_script( 'algolia-instantsearch' );
// WordPress utility useful for using underscore templating.
wp_enqueue_script( 'wp-util' );
// Allow users to easily enqueue custom styles and scripts.
do_action( 'algolia_instantsearch_scripts' );
} );
Then just add the instantsearch.php code or include the file to the index page/page you want to load it on.
(I just replaced the index.php code with the code from instantsearch.php and it worked just fine)
Hope that helps.

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 plugin add page frontend

I'm developing a plugin for wordpress. This plugin has to have an admin section for plugin set up but also has to have a custom front end page with forms.
I'm new into the wordpress plugin development world but I have not found specific information for this task.
Is there a way to add pages to front end from a plugin or is necessary to manually edit the current template and add the page?
Here is a way to add custom content on a front-end page when you create your plugin: http://blog.frontendfactory.com/how-to-create-front-end-page-from-your-wordpress-plugin/
function elegance_referal_init()
{
if(is_page('share')){
$dir = plugin_dir_path( __FILE__ );
include($dir."frontend-form.php");
die();
}
}
add_action( 'wp', 'elegance_referal_init' );
To add a page to Wordpress that runs custom code without adding the page as a post or page in the database, try this in your plugin file.
add_action ('pre_get_posts', function ($query) {
global $wp;
if ($wp->request == 'pageurl'){
$dir = plugin_dir_path( __FILE__ );
include $dir . "your_file.php";
die();
}
});
I think you looking for this for the admin settings page https://codex.wordpress.org/Function_Reference/add_options_page
Fot the front end, i would recommend to create a page with a template https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/

Why does wpmu_new_user not fire inside of a plugin but does inside functions.php?

I have a project to complete where sites within a WordPress Multisite blog will automatically add users to the corresponding sites (where the plugin is enabled)
I hope I worded that correctly.
The problem: The wpmu_new_user hook does not fire inside of a plugin but WILL inside of functions.php
This is my code:
add_action( 'wpmu_new_user', 'register_hack_action', 10, 1 );
add_action( 'wpmu_activate_user', 'register_hack_action', 10, 1);
function register_hack_action( $user_id ) {
$this_id = get_current_blog_id();
if ( !defined('ABSPATH') ) {
// do nothing
} else {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
switch_to_blog($blog['blog_id']);
if ( is_plugin_active( 'register-hack/register-hack.php' ) ) {
// add user to blog
add_user_to_blog($blog['blog_id'], $user_id, 'subscriber');
}
}
}
This works perfectly fine when you add the snippet to functions.php. But when you add it to a plugin (which goes into wp-content/plugins) and then is activated on certain sites, just will not work. if you can try it for yourself you'll see what i mean.. I don't understand why it will not work. I need it to be inside of the plugin and work.
Only some extensive debugging could answer this... But this kind of stuff is better placed inside a Must Use plugin.
Must-use plugins (a.k.a. mu-plugins) are plugins installed in a special directory inside the content folder and which are automatically enabled on all sites in the installation. Must-use plugins do not show in the default list of plugins on the Plugins page of wp-admin – although they do appear in a special Must-Use section – and cannot be disabled except by removing the plugin file from the must-use directory, which is found in wp-content/mu-plugins by default.
I don't understand why you're using that include_once, please, test it removing that.
Especulations:
as a normal plugin, it should be Network Activated and try to encapsulate the actions with:
add_action( 'plugins_loaded', function() {
add_action( 'wpmu_new_user', 'register_hack_action', 10, 1 );
add_action( 'wpmu_activate_user', 'register_hack_action', 10, 1);
});
maybe you're putting inside the main site theme?

Categories