wordpress: detect plugins and themes screens activation - php

I need to detect when user goes to :
Plugins -> Installed Plugins and Appearance -> Themes
in wordpress admin.
Something like :
add_action("core_upgrade_preamble", "action_core_upgrade_preamble")

You have 2 options:
Option 1:
Use the callback $hook, first get the $hook of the page using this:
function load_custom_wp_admin_style($hook) {
var_dump($hook);
}
add_action( 'admin_init', 'load_custom_wp_admin_style' );
Check your page in the browser you should get the string name, then you can use:
function load_custom_wp_admin_style($hook) {
// Load only on my specific page
if($hook != 'page_hook_i_got') {
return;
}
//DO MY LOGIC
}
add_action( 'admin_init', 'load_custom_wp_admin_style' );
Option 2:
Use get_current_screen();
add_action( 'current_screen', 'this_screen' );
function this_screen() {
$current_screen = get_current_screen();
if( $current_screen ->id === "widgets" ) {
// Run some code, only on the admin widgets page
}
}
you will need to do a var_dump to find the id of the page, like in option 1, you can find more info here.

Related

How to rename a menu tab under WooCommerce tab on WordPress admin dashboard

I need help in renaming a tab menu item under woocommerce tab on wordpress admin. We installed a plugin that appears as a submenu on woocommerce tab. Can anyone please help me on this?
I found this code below to rename a tab menu, but I dont know what is the tabmenu key of it. Or anyone here how to check tab menu key on my current tab menu items?
add_action( 'admin_menu', 'custom_change_admin_label', 99);
function custom_change_admin_label() {
global $menu;
//global $submenu;
$menu[5][0] = 'Articles';
}
Thank you
The first part of the code is to debug, this will show you the menu in detail on the dashboard. (you can remove this afterwards)
The 2nd part in this example changes the 'coupons' label to 'voucher'
It is therefore a matter of adjusting based on the detail
// DEBUG: This displays the complete wordpress admin menu on your dashboard for admin only.
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
// Change label, in this example changes the 'coupons' label to 'voucher'
function custom_change_admin_label() {
global $menu, $submenu;
$submenu['woocommerce'][2][0] = 'Voucher'; // rename 'coupons' label
}
add_action( 'admin_menu', 'custom_change_admin_label' );

Disable Jetpack Carousel on specific pages in WordPress

I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.

Wordpress dequeue scripts without handle?

I'm currently using Foobox lightbox (free) plugin, and apparently the plugins' files are loaded on every page regardless of whether it's used or not. However I would like to change this, but I can't seem to find any handles to dequeue the scripts through.
It seems that the scripts are called by the code beneath, which to me at least, doesn't show any handles. I've tried using remove_action(...) to "counter" them as well as various inputs in wp_dequeue_script() to try and target the files directly - e.g. wp_dequeue_script('foobox.free.min.js')
class Foobox_Free extends Foo_Plugin_Base_v2_1 {
const JS = 'foobox.free.min.js';
const CSS = 'foobox.free.min.css';
const CSS_NOIE7 = 'foobox.noie7.min.css';
const FOOBOX_URL = 'http://fooplugins.com/plugins/foobox/?utm_source=fooboxfreeplugin&utm_medium=fooboxfreeprolink&utm_campaign=foobox_free_pro_tab';
const BECOME_AFFILIATE_URL = 'http://fooplugins.com/affiliate-program/';
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Foobox_Free ) ) {
self::$instance = new Foobox_Free();
}
return self::$instance;
}
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*/
private function __construct() {
//init FooPluginBase
$this->init( FOOBOXFREE_FILE, FOOBOXFREE_SLUG, FOOBOX_BASE_VERSION, 'FooBox FREE' );
if (is_admin()) {
add_action('admin_head', array($this, 'admin_inline_content'));
add_action('foobox-free-settings_custom_type_render', array($this, 'custom_admin_settings_render'));
new FooBox_Free_Settings();
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_GETTING_STARTED, array( $this, 'render_page_getting_started' ) );
add_action( FOOBOX_ACTION_ADMIN_MENU_RENDER_SETTINGS, array( $this, 'render_page_settings' ) );
add_action( 'admin_notices', array( $this, 'admin_notice_foogallery_lightboxes' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_ignore_notice', array( $this, 'admin_notice_foogallery_lightboxes_ignore' ) );
add_action( 'wp_ajax_foobox_foogallery_lightboxes_update', array( $this, 'admin_notice_foogallery_lightboxes_update' ) );
add_action( 'admin_print_scripts', array( $this, 'admin_notice_foogallery_lightboxes_inline_js' ), 999 );
add_filter( 'foobox-free-has_settings_page', '__return_false' );
} else {
// Render JS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
add_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
// Render CSS to the front-end pages
add_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
if ( $this->is_option_checked('disable_others') ) {
add_action('wp_footer', array($this, 'disable_other_lightboxes'), 200);
}
}
}
How, if possible, do I dequeue these scripts (and styles) without editing the plugin file directly?
Edit
Below I've added the things I've tried doing to remove the scripts (all added to functions.php):
add_action( 'wp_enqueue_scripts', 'remove_foobox_scripts', 100 );
function remove_foobox_scripts() {
if ( !is_page('my-page') ) {
wp_deregister_script( 'foobox.free.min.js' );
wp_dequeue_script( 'foobox.free.min.js' );
}
}
Also tried the below, which is just a straight copy from the foobox file:
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_scripts'), 20);
remove_action('foobox-free_inline_scripts', array($this, 'inline_dynamic_js'));
remove_action('wp_enqueue_scripts', array($this, 'frontend_print_styles'));
Also tried the below, where the array( part is removed:
remove_action('wp_enqueue_scripts','frontend_print_scripts', 20);
remove_action('foobox-free_inline_scripts', 'inline_dynamic_js');
remove_action('wp_enqueue_scripts', 'frontend_print_styles');
The problem with the way you are trying to do this stems from the order in which things happen in Wordpress.
You are relying on the conditional tags like is_page('my-page') to determine whether or not to load the plugin. These conditional tags do not become available until Wordpress has parsed the URL for the current query, and at this point all the plugins and your theme have already been loaded. Even if you parse the URL yourself instead of using the conditional tags you cannot be sure your code will run before the plugins are loaded.
The solution is to add your code as an mu-plugin. These are loaded before normal plugins so you can use an option (option name) filter here to alter the plugins you want to be loaded.
Option filters pass an array to your function containing the values which are set for that option, so in this case you want to hook option_active_plugins.
You can find the values to use for by running print_r(get_option('active_plugins')); or look through the plugins folder of your wordpress install.
The following example is specific to your question, but you could modify it to make a more comprehensive set of rules, adding and removing multiple plugins on different pages based on many conditions.
My function checks you are not in wp-admin and then has 2 conditions. The first disables a normally active plugin on the specified pages, the second enables a normally disabled plugin on the specified pages.
<?php
add_filter( 'option_active_plugins', 'add_or_remove_plugins' );
function add_or_remove_plugins( $plugins ) {
if (strpos( $_SERVER['REQUEST_URI'], '/wp-admin/' ) !== 0) { // Disable in admin pages or admin plugin settings stop working properly
if (strpos( $_SERVER['REQUEST_URI'], '/remove-plugin-here/' ) === 0) { // Conditonal tags still unavailable so you have to parse urls yourself
$k = array_search( 'foobox-image-lightbox/foobox-free.php', $plugins ); // This will stop an active plugin from loading
if( false !== $k ){
unset( $plugins[$k] );
}
}
if (strpos( $_SERVER['REQUEST_URI'], '/add-plugin-here/' ) === 0) {
$plugins[] = 'foobox-image-lightbox/foobox-free.php'; // This will load the plugin along with all the active plugins
}
}
return $plugins;
}
?>
To install just change the values to suit, paste into a file and upload into your mu-plugins folder
EDIT
Looks like your inline js is added to wp_head during the constructor of the Foobox_Free class. You could try adding this to your functions.php:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
$foo = Foobox_Free::getInstance();
remove_action('wp_head', array($foo, 'inline_dynamic_js'));
}
or if that doesn't work then maybe this:
add_action( 'wp_head', 'remove_dynamic_js' );
function remove_dynamic_js(){
remove_action('wp_head', array('Foobox_Free', 'inline_dynamic_js'));
}
The action is added inside a private function, so I don't know if either of those will actually work. Give it a shot. If not my first answer will as it stops the plugin from loading at all on the specified pages.
UPDATE
Well, I was close... Here's the code to remove the scripts, as supplied by the plugin author.
$foobox = Foobox_Free::get_instance();
remove_action('foobox-free_inline_scripts', array($foobox, 'inline_dynamic_js'));

How to remove adminbar in Wordpress on certain pages

I try to remove the toolbar/adminbar on my main page when users are logged in but it doesn't work properly. Actually it doesn't work.
I found this code :
add_action( 'after_setup_theme', 'my_website_remove_admin_bar' );
function my_website_remove_admin_bar() {
// hide the admin bar on your main page
if ( is_home() )
show_admin_bar( false );
}
I try to have it out of my homepage...
The show_admin_bar filter would be much more appropriate in this case:
function conditional_hide_admin_bar() {
return is_front_page() ? false : true;
}
add_filter( 'show_admin_bar', 'conditional_hide_admin_bar' );

How to create a setting page for my custom plugin?

I have created this custom plugin and after the activation it shows "settings" on the installed plugin.
I do not want to show my setting page LINK anywhere in the the admin panel menu but when user clicks the setting of the plugin (see the above screen shot) that should go to the plugin setting page. I understand that I have to do something on my second function "our_plugin_action_links" 's variable $settings_link
I just tried to link a single php file that placed on the admin folder. Then it goes to that php file when clicks. But I want to show the plugin setting page inside the admin panel same like other pages such as add post or **general setting page when click setting of the plugin but not showing the links or menus at the admin menu for the plugin setting.
How can I do that?
my plugin code
<?php
/*
Plugin Name: admin menu remover
Description: Remove the admin menus just by a single plugin installation
Version: 1.0
Author: Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
/* This function and action removes following menu item from the admin panel */
add_action( 'admin_menu', 'remove_links_menu' );
function remove_links_menu() {
remove_menu_page('index.php'); // Dashboard
//remove_menu_page('edit-comments.php'); // Comments
remove_menu_page('themes.php'); // Appearance
//remove_menu_page('plugins.php'); // Plugins
//remove_menu_page('tools.php'); // Tools
//remove_menu_page('options-general.php'); // Settings
//remove_menu_page('users.php'); // Users
}
/* This function and filter append "setting" immediately after the "admin menu remover" plugin activation */
add_filter('plugin_action_links', 'our_plugin_action_links', 10, 2);
function our_plugin_action_links($links, $file) {
static $this_plugin;
if (!$this_plugin) {
$this_plugin = plugin_basename(__FILE__);
}
// check to make sure we are on the correct plugin
if ($file == $this_plugin) {
// the anchor tag and href to the URL we want. For a "Settings" link, this needs to be the url of your settings page
$settings_link = 'Settings';
// add the link to the list
array_unshift($links, $settings_link);
}
return $links;
}
?>
First off, from a user's perspective I wouldn't recommend doing this. If your plugin warrants a settings page then put it where settings below. If you don't trust your users then don't allow the settings OR use capabilities to restrict your menu to only certain users.
However, if you've got a specific need for this then the easiest way is to just register a normal options page using add_options_page and then manually rip your menu out of the global array. This method ensures that permissions are accounted for correctly and is pretty safe.
Also, instead of plugin_action_links you can use the filter that is specific to your plugin via 'plugin_action_links_' . plugin_basename( __FILE__ ) which is what the code below does. You'll need to change the constant to something more specific to your code (or switch to a global variable or just use strings). See the code comments for more details.
//We'll key on the slug for the settings page so set it here so it can be used in various places
define( 'MY_PLUGIN_SLUG', 'my-plugin-slug' );
//Register a callback for our specific plugin's actions
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'my_plugin_action_links' );
function my_plugin_action_links( $links )
{
$links[] = 'Settings';
return $links;
}
//Create a normal admin menu
add_action( 'admin_menu', 'register_settings' );
function register_settings()
{
add_options_page( 'My Plugin Settings', 'My Plugin Settings', 'manage_options', MY_PLUGIN_SLUG, 'my_plugin_settings_page' );
//We just want to URL to be valid so now we're going to remove the item from the menu
//The code below walks the global menu and removes our specific item by its slug
global $submenu;
if( array_key_exists( 'options-general.php' , $submenu ) )
{
foreach( $submenu['options-general.php'] as $k => $v )
{
if( MY_PLUGIN_SLUG === $v[2] )
{
unset( $submenu['options-general.php'][$k] );
}
}
}
}
//This is our plugins settings page
function my_plugin_settings_page()
{
echo 'Hello from my plugin!';
}
You can try the generator on this site.
http://wpsettingsapi.jeroensormani.com/
What it does is you make a callback function where you set your options. So not a seperate file but just your functions.php or plugin file.
Or checkout the codex http://codex.wordpress.org/Creating_Options_Pages

Categories