Why remove_meta_box() function is not working in my plugin? - php

Im trying to delete the section WordPress News in my dashboard by creating a simple plugin. First of all, i've made a php file in the following directory:
wp-content/plugins/basic-plugin.php
In the basic-plugin.php file i have the following block of code:
<?php
/*
Plugin Name: Basic Plugin
Plugin URI: http://wordpress.org/plugins/basic-plugin/
Description: My first plugin for creating and displaying job opportunities
Author: Rumen Panchev
Version: 1.0
License: GPLv2
*/
function ru_remove_dashboard_widget() {
remove_meta_box( 'dashboard_primary', 'dashboard', 'post_container_1' );
}
add_action( 'wp_dashboard_setup', 'ru_remove_dashboard_widget' );
The problem is that the section is still in the dashboard. Im new in WordPress Development and not sure why is this happening ?

Your approach is correct however you're using the wrong context argument with remove_meta_box(). Dashboard widgets aren't registered under post_container_1.
Valid options for the context argument include 'normal', 'advanced' or 'side'. For the particular dashboard widget you're attempting to remove, you need 'side'.
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
Documentation: https://codex.wordpress.org/Function_Reference/remove_meta_box

Related

Woocommerce conditionally load a custom product template

I am creating a custom theme and I have copied the file content-product.php to my theme folder. There I have made changes to the html structure and css. I can load of list of products and see the changes working via the short code [products].
However elsewhere on the site I want to display another list of products but from a different category. I would use the shortcode [products category="special category"] These products should be displayed using a different template.
My question is: Where can I inspect the shortcode query? and how can I conditionally load a different template depending on which products are being displayed?
In my themes functions.php file I have started to extend the [products] shortcode like this:
function my_wc_shortcode_product_query_args($args, $atts){
if ( isset( $args['category'] ) && $args['category'] == "Daoine Óga") {
var_dump($args);
// Tell Woocommerce to load my custom template instead of the my-theme/woocommerce/content-product.php
}
return $args;
}
But I'm not sure how I can return or get woocommerce to display my custom template at this point.
The woocommerce file that creates the [products] shortcode can be found at plugins/woocommerce/includes/shortcodes/class-wc-shortcode-products.php
Make a folder in plugins folder called custom-product-templates and make a copy of the woocommerce file class-wc-shortcode-products.php to that folder.
Add the plugin comments to the top of that file:
/*
Plugin name: Custom Product Template
Plugin URI: https://anirishwebdesigner.com
Description: Display custom product templates in woocommerce loop
Author: Linda Keating
Author URI: http://anirishwebdesigner.com
Version: 1.0
*/
Navigate to localhost/wp-admin in browser and to the plugins and activate newly created plugin
I can now safely edit that file so that the shortcode behaviour loads the template I want based on certain criteria.
First extend the class so that it accepts a template attribute
In the parse attributes function add
'template' => '',
Then search for wc_get_template_part('content', 'product'); and modify the code here to load different templates in an if..else statement. Something like:
`if($this->attributes['template'] == 'Daoine Óga') {
wc_get_template_part( 'content', 'childrenproduct' );
} else {
wc_get_template_part( 'content', 'product' );
}`
If there are simpler / better solutions please let me know.

WordPress independent functions.php

I have a fresh WordPress and bbPress installed on an internal server.
While I was setting up bbPress I wanted to test the functionalities like creating a forum, topic, etc. When doing these stuff in the back-end (dashboard) there didn't seem to be any problems but when I did it from the front-end I kept getting
ERROR: Are you sure you wanted to do that?
I searched for a solution and found this.
add_filter( 'bbp_verify_nonce_request_url', 'my_bbp_verify_nonce_request_url', 999, 1 );
function my_bbp_verify_nonce_request_url( $requested_url )
{
return 'http://localhost:8888' . $_SERVER['REQUEST_URI'];
}
I simply changed the hard-coded URL to what our internal server is set up to and it fixed the problem.
Now my question: is it possible for me to add this solution to a functions.php that is independent of the theme being used? I asked this because I have 2 concerns:
The current theme might get updated and will overwrite the file
I'm aware that the solution to this is simply create a child theme but my second concern prevents me from doing this.
The WordPress administrator might change themes and so both the functions.php file on the main theme and the child theme will stop working
How could I add the solution above so that I don't have to worry about the theme being updated and/or replaced with a new theme in the future? I don't want to keep adding this solution every time the administrator decides to change themes.
If you can't put it in a theme, put it in a Plugin. If you're worried that the plugin will get de-activated make it a Must Use Plugin.
It's dead simple to create a plugin. Create a file plugin-name.php and place it in a directory wp-content/plugins/plugin-name/. That file should contain the following code:
<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
add_filter( 'bbp_verify_nonce_request_url', 'my_bbp_verify_nonce_request_url', 999, 1 );
function my_bbp_verify_nonce_request_url( $requested_url )
{
return 'http://localhost:8888' . $_SERVER['REQUEST_URI'];
}
If you want it to be a must use plugin, put it in wp-content/mu-plugins/ instead of wp-content/plugins.

Wordpress custom plugin

so I am trying to understand the Wordpress custom plugin creation. I have enabled it to activate and show itself with an icon in the menu but I just need to know how to separate the function from the admin dashboard page and what outputs on the theme.
I created a directory for the plugin and two files inside that, one being the functional file and one as included file to separate the code to make it easier to read when creating the config page for the plugin in the dashboard.
<?php
/*
Plugin Name: Feather Slider
*/
add_action( 'admin_menu', 'register_feather_slider' );
function register_feather_slider(){
add_menu_page(
'feather_slider',
'Feather Slider', 'manage_options',
'feather_slider', // URL Slug
'feather_slider', // Menu Label
'dashicons-images-alt2', 50); // Menu icon & Menu Location
}
function feather_slider(){
include('admin_index.php'); // I want this to handle the plugin config in the dashboard
add_shortcode('test', 'feather_slider'); // I want this to out put a file which creates the slider.
}
?>

Override a wordpress theme language file using a custom plugin

I am using a wordpress theme that contains several language files. I would like to modify a language file to "white label" the theme I am using. I will also be making other admin-css changes to the theme, therefore, I'd like to create a plugin that allows me to do all of this.
What I am unsure of how to do is create a custom plugin that would override a themes language file - is this possible?
I have so far tried this:
<?php
/*
Plugin Name: Layers Whitelabel test
Plugin URI: http://www.skizzar.com
Description: Plugin to whitelabel layers for Skizzar
Author: Skizzar
Version: 1.0
Author URI: http://www.skizzar.com
*/
function set_myplugins_languages() {
load_textdomain( 'layerswp', plugin_dir_url( __FILE__ ) . '/language' );
}
add_action('init', 'set_myplugins_languages');
But so far no joy - as in, it doesn't load the new language file in place of the existing one.
Anyone got any ideas on how this can be achieved?
Well, why do you need a plugin for this?
The theme style.css have similar code to
Plugin Name: Layers Whitelabel test
Plugin URI: http://www.skizzar.com
Description: Plugin to whitelabel layers for Skizzar
Author: Skizzar
Version: 1.0
Author URI: http://www.skizzar.com
So you can simply change theme display name, url, author etc.
Basically it's easy to do, however it's against theme copyrighting ;) Even if this is a GPL theme you should refer to original developer.

How to convert a Shortcode into a Plugin

1. Shortcode (working)
I have made a Shortcode which display the current weatherinformations from yahoo. There was 3 Files (simple-pie.inc, simple-pie-yahoo.inc and yahooweather.php) and I grabed the content of these files and pasted it inside my function.php – works good!
This looks so and got more then 16.000 Lines of Code:
// Add Shortcode for weather
function get_rohrau_wetter() {
content of simple-pie.inc
content of simple-pie-yahoo.inc
content of yahooweather.php -> return "weather-html"
}
add_shortcode( 'rohrauwetter', 'get_rohrau_wetter' );
2. Plugin (no clue where to start)
All I want to do is making a Plugin out of my working shortcode to get that "wall-of-code" out of my function.php and of course for better reuseability. Also I think so it will be possible to turn the simple-pie cache to "on" for faster processing…
My Question is: How do I convert my Shortcode into an Plugin?
A good place to start would the plugin handbook: https://developer.wordpress.org/plugins/the-basics/header-requirements/
But the cheap and nasty way to place it into a plugin, create a directory for your plugin, create a index.php (realistically it can be any name) file. Add this in
<?php
/*
Plugin Name: WordPress.org Plugin
Plugin URI: https://developer.wordpress.org/plugins/the-basics/
Description: Basic WordPress Plugin Header Comment
Version: 20160911
Author: WordPress.org
Author URI: https://developer.wordpress.org/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wporg
Domain Path: /languages
*/
Then underneath it add in your above functions. Obviously Change out the text to suit yourself so that you can identify the plugin. Then upload it too your plugins directory.
As I said, this is the cheap and nasty way to set it up. I would advise to have a look at the plugin handbook I linked to above. Also Using OOP plugin development is advised, a good starting point is Tom McFarlin tutorial on tutsplus: https://code.tutsplus.com/series/object-oriented-programming-in-wordpress--cms-699 .. and after that well worth looking at https://github.com/DevinVinson/WordPress-Plugin-Boilerplate and https://wppb.me/ hope that all helps.

Categories