Community Forums › Forums › Archived Forums › Design Tips and Tricks › Disable Plugins From Loading on Specific Pages – Multiple URLs
Tagged: php array
- This topic has 4 replies, 2 voices, and was last updated 6 years, 3 months ago by Brad Dalton.
-
AuthorPosts
-
December 21, 2017 at 4:55 am #214715netprojectParticipant
Hello everyone,
To disable WordPress plugins from loading on specific pages and posts, I'm using a MU-Plugin code suggested on a kinsta.com post (see GitHub below). This way the contact-form-7 plugin, for example, will only be loaded on the contact page.
Let's say, I want to use a contact form on 2 other pages and disable it on all other pages not using a contact form.
Being not a developer, I'm struggling with syntax and logic here when trying to add multiple URLs to the code below. I tried to add multiple URLs in two tests, which however resulted in syntax or parse errors.
Test 1:
$is_contact_page = strpos( $request_uri, '/contact/', 'contact2', '/contact3' );Test 2:
$is_contact_page = strpos( $request_uri, '/contact/' );
$is_contact_page = strpos( $request_uri, '/contact2/' );
$is_contact_page = strpos( $request_uri, '/contact3/' );My Question
Does someone know or can suggest a solution on how to add multiple URLs here? I would like to allow the contact form to be loaded on those defined pages, but disable it on all others.Ps: I have tested and installed the Plugin Organizer plugin, but for some websites I'd prefer a programming solution without installing this plugin.
Code example from Kinsta.com
<?php // returns the path of the request URI without the query string // see http://php.net/manual/en/function.parse-url.php // and http://php.net/manual/en/reserved.variables.server.php // and http://php.net/manual/en/url.constants.php $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); $is_admin = strpos( $request_uri, '/wp-admin/' ); // add filter in front pages only if( false === $is_admin ){ add_filter( 'option_active_plugins', 'kinsta_option_active_plugins' ); } /** * Filters active plugins * * @param array $plugins An array of active plugins. */ function kinsta_option_active_plugins( $plugins ){ global $request_uri; $is_contact_page = strpos( $request_uri, '/contact/' ); $unnecessary_plugins = array(); // conditions // if this is not contact page // deactivate plugins if( false === $is_contact_page ){ $unnecessary_plugins[] = 'contact-form-7/wp-contact-form-7.php'; $unnecessary_plugins[] = 'custom-post-type-ui/custom-post-type-ui.php'; $unnecessary_plugins[] = 'query-monitor/query-monitor.php'; } foreach ( $unnecessary_plugins as $plugin ) { $k = array_search( $plugin, $plugins ); if( false !== $k ){ unset( $plugins[$k] ); } } return $plugins; }
Resources
https://kinsta.com/blog/disable-wordpress-plugins/Thanks a lot in advance,
MarkDecember 21, 2017 at 5:40 am #214716Brad DaltonParticipantDecember 21, 2017 at 7:47 am #214720netprojectParticipantThanks Brad!
As said, I'm not a developer, so I tried to follow the W3Schools example, and how the array is being used for the multiple plugins within the code example above, but not sure.
Would the array for the URL sequence look like this?
function kinsta_option_active_plugins( $plugins ){ global $request_uri; $is_contact_page = array(); { $is_contact_page = strpos( $request_uri, '/contact/' ); $is_contact_page = strpos( $request_uri, '/contact1/' ); $is_contact_page = strpos( $request_uri, '/contact2/' ); } . . .
Thanks Mark
December 31, 2017 at 12:05 pm #214992netprojectParticipantJust wondering, if anyone has got more ideas on this?
December 31, 2017 at 9:28 pm #215007Brad DaltonParticipantI don't think you need to do this because the code only executes on pages containing the shortcode.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.