Question about WordPress theme - plugins interaction.
Can i unregister widget that was added by plugin using theme functions.php file?
Tried to unregister it using sample code, but it didn't work for me:
function remove_some_widget() {
unregister_widget('some_plugin_widget');
}
add_action( 'widgets_init', 'remove_some_widget' );
The parameter to pass in unregister_widget function is the name of a class that extends, so, pass the appropriate class name. Here is an example of unregister_widget to Unregister all widgets
function unregister_default_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
unregister_widget('WP_Widget_Text');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_Recent_Posts');
unregister_widget('WP_Widget_Recent_Comments');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Tag_Cloud');
unregister_widget('WP_Nav_Menu_Widget');
unregister_widget('Twenty_Eleven_Ephemera_Widget');
}
add_action('widgets_init', 'unregister_default_widgets', 11);
Related
I need to create a custom function for plugin wp-estore . Function should be added on function.php of Theme and should be called at plugin.
Plugins are loaded before the theme so the function hasn't been defined at the time you're calling it. Execute your code on a hook such as init instead.
function wpse_my_plugin_init() {
myPreviouslyUndefinedThemeFunction();
}
add_action( 'init', 'wpse_my_plugin_init' );
I have a plugin, it has many css theme files. Of course I do not want to load all of them, only one. It depends on config. For post I use has_shortcode function, but how todo the same thing with template that use do_shortcode function.
Note:
I found a good solution, I use
$this->loader->add_action( 'init', $plugin_public, 'register_scripts');
$this->loader->add_action( 'wp_footer', $plugin_public, 'print_scripts');
Inside shortcode handle I set a global var to true
global $imagelink_plugin_shortcode_used;
$imagelink_plugin_shortcode_used = true;
The function print_scripts add my scripts if my global var is true
public function print_scripts() {
global $imagelink_plugin_shortcode_used;
if ( ! $imagelink_plugin_shortcode_used )
return;
wp_print_scripts($this->plugin_name . '-imagelinks');
}
Thanks for answers.
Instead of using has_shortcode function, what you can do is register and enqueue those files when that shortcode is rendered.
First, register your css files with wp_register_script. Make sure to hook this into wp_enqueue_scripts
Now, inside your shortcode function, enqueue the files.
wp_enqueue_style('theme-css')
Using this way you can use the shortcode anywhere you want and the script is loaded only when shortcode is present on the page whether it is on content or template.
I am looking for a way to hide activity feed from the dashboard using a function. Does anyone know how to do this? I want to completely remove it. I want to achieve this without a plugin.
You can use remove_meta_box() like;
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
add above code to functions.php
Dashboard widgets and other meta boxes can also be removed by using the unset function. You might need to play around with the array keys, or use var_dump() to find the path for the widget you're looking for.
// Removes dashboard activity widget.
function remove_dashboard_activity_widget() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
}
// Triggers dashboard widgets removal.
add_action('wp_dashboard_setup', 'remove_dashboard_activity_widget');
Additionally like Hüseyin BABAL mentioned, meta boxes can also be removed like this:
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
You'll either have to create a plugin or add a function to your theme functions.php file.
function remove_activity_dashboard_widget() {
remove_meta_box( 'dashboard_activity', 'dashboard', 'side' );
}
// Hook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'remove_activity_dashboard_widget' );
Here is the codex page on the subject:
http://codex.wordpress.org/Dashboard_Widgets_API#Advanced:_Removing_Dashboard_Widgets
I was wondering how to remove the posts section from the wordpress admin sidebar (see image below)
you will be needing to edit functions.php for this and add some code in that. This section of posts lies as edit.php
See the official wordpress codex documentation for remove_menu_page() function to understand better. Doc states function usage as:
<?php remove_menu_page( $menu_slug ) ?>
Here $menu_slug is edit.php for post menu.
Now create your own function called post_remove() and add code in functions.php as:
function post_remove ()
{
remove_menu_page('edit.php');
}
The next part is to hook your post_remove() function with a specific action which in this case is admin_menu to trigger this function. For that, add some more code in functions.php:
add_action('admin_menu', 'post_remove');
So in short, following is complete code that you need to add in your functions.php file:
function post_remove () //creating functions post_remove for removing menu item
{
remove_menu_page('edit.php');
}
add_action('admin_menu', 'post_remove'); //adding action for triggering function call
Official documentation links
http://codex.wordpress.org/Function_Reference/remove_menu_page
http://codex.wordpress.org/Function_Reference/add_action
I hope this helps! Please do me a favor - vote up for my answer and mark it accepted.
add this function to your functions.php
function remove_menu ()
{
remove_menu_page('edit.php');
}
add_action('admin_menu', 'remove_menu');
I'm developing a WordPress widget following Dave Clements tutorial. It works well. Now I want to add some styles to it. I want the styles to be in an extra css file which will be loaded during runtime. I'm calling this function
function myprefix_add_my_stylesheet() {
wp_register_style( 'myprefix-style', plugins_url('mystyle.css', __FILE__) );
wp_enqueue_style( 'myprefix-style' );
}
right before "// Widget output //" using this statement.
add_action( 'wp_enqueue_scripts', 'myprefix_add_my_stylesheet' );
But nothing seems to happen. What am I doing wrong?
Your wp_enqueue_scripts action do not work because you call it too late, in widget() function (widget output). It's too late because Wordpress already print/send head of page.
For example, you can add this action on widget construct function.