Wordpress remove_meta_box issue - wordpress

I run into a problem trying to get remove_meta_box() to work
/** remove metabox for catchkathmandu options
*/
function vpm_remove_meta_box() {
remove_meta_box( 'catchkathmandu-options', 'post', 'normal' );
remove_meta_box( 'authordiv' , 'page' , 'normal' ); //removes author
}
add_action( 'add_meta_boxes', 'vpm_remove_meta_box' );
The point is I dont want contributors and authors to edit the site design, so I wanted to remove the catchkathmandu-options. the code obviously does not reflect the condition, the codex for this function supplies the solutions for that but this code at the besic step, still isnt working - I switched back to Twenty Sixteen theme and put in the authordiv section simply to test. But still no dice.
It's added to child theme functions.php
Have I taken the wrong path and am I looking at the wrong thing entirely?
Any help appreciated!

I suppose that you are using a theme called "Catch katHmandu", and you want to remove a meta box for everyone except Admins.
Here is the usage of remove meta box:
remove_meta_box( $id, $page, $context );
And the Reference on the codex: https://codex.wordpress.org/Function_Reference/remove_meta_box
As you can read on the codex, $id is the current id of the div that you want to remove. Let's say that you want to remove "Tags box", if look for its container id you will find:
<div id="tagsdiv-post_tag" class="postbox">
...
</div>
And if you want to remove tags meta box, your code will look like this:
if (!is_admin()) :
function my_remove_meta_boxes() {
remove_meta_box('tagsdiv-post_tag', 'page', 'normal');
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
endif;
Look for the id of the meta box that you want to remove and remplace "tagdiv-post_tag" from code above with your id.
If you like my answer click the top arrow, as a coder it would mean a lot for me. Thanks!
EDIT:
As #AndrewSeabrook say, try using admin_menu hook instead of add_meta_boxes in your function.

Related

Remove description meta box from custom taxonomy

I'm trying to remove these areas from one of my custom taxonomies.
I've built them using the two plugins: Custom Post Types UI (to add them) and Advanced Custom Fields (to add fields to the taxonomy).
I can't see anything in the plugin settings to remove these things, but I'm not sure if I'm missing something.
I'm assuming I might need to add a function to the functions.php file. I've seen that hiding things using jQuery is a possibility, but I hear that this might show it initially on load and then hide it, so id like to learn how to doit properly.
I managed to get there with the link above and help from another website.
This function removed the two instances of the 'description' box:
function hide_description_row() {
echo "<style> .term-description-wrap { display:none; } </style>";
}
add_action( "measure_sectors_edit_form", 'hide_description_row');
add_action( "measure_sectors_add_form", 'hide_description_row');
and this one removed the column from the right hand side:
add_filter('manage_edit-measure_sectors_columns', function ( $columns ) {
if( isset( $columns['description'] ) )
unset( $columns['description'] );
return $columns;
});
To use with other taxonomies, just replace 'measure_sectors' with your own taxonomy slug

Show content before main content based on color attribute

I found a code snippet to display the content before the main content and it worked.
Currently the content is displayed on all pages. (except shop page)
The code :
add_action( 'woocommerce_before_main_content', 'BannerShop', 35 );
function BannerShop(){
if(!is_shop()){
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
}
}
What I want to ask is, how to display content only for color attribute products in the form of links.
Example :
The display (content) will ONLY SHOW when the url is like this :
mysite.com/color/red/
Sorry if the explanation is not good because I don't really understand this.
Any help is greatly appreciated.
thank you
I understand your question is about displaying that extra content, if the current query is for a product archive page only showing products of a certain attribute 'color'.
Each WooCommerce attribute is an independent taxonomy.
WordPress's is_tax('parameter') function checks, if the query is for an existing custom taxonomy archive page (other than category & tag) & if the query is for that specific taxonomy 'parameter', in your case 'color'.
So, this code snippet in your functions.php or equivalent plugin should work:
add_action( 'woocommerce_before_main_content', 'BannerShop', 35 );
function BannerShop(){
(is_tax('color')) {
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
}
}
Though, to make the above template WooCommerce override work, declare WooCommerce support for your theme by adding the following lines to your functions.php or plugin equivalent:
function theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'theme_add_woocommerce_support' );

remove_action hook not working to remove credit card fields from donation form GiveWP

I have used below action hook to hide fields but it is not working.
remove_action( 'give_cc_form', 'give_get_cc_form' );
can anyone help me to figure out this issue
I am using GiveWP plugin for donation in WordPress site.
GiveWP has a pretty expansive snippet library, and one example shows how you can remove and rearrange fields. This is probably the best place to start from:
https://github.com/impress-org/givewp-snippet-library/blob/master/form-customizations/customize-fieldset-order.php
function give_remove_fieldsets() {
remove_action( 'give_cc_form', 'give_get_cc_form' );
}
add_action( 'init', 'give_remove_fieldsets' );
this actually worked for me
Make sure to run it at init, and you need to add the same priority used in the add_action, if none it uses 10 and the priority can be left blank
add_action('init','remove_actions');
function remove_actions() {
remove_action( 'give_cc_form', 'give_get_cc_form', 1 ); // Replace the priority with the correct one
}

Wordpress custom meta box for one page only

everyone!
I believe similar question have been asked already, but I had not found working solution.
The problem: I need a metabox for one exact page only.
The code I use to add a metabox is pretty simple:
function custom_meta_boxes(){
add_meta_box('custom_meta_1', 'About Us Main field','custom_meta_boxes_render',
'page','normal','high','');
}
If you look on the 4th parameter - 'page', this puts metabox on every page, so that I see it and can use it for every page I created when editing it. And I need to see it on about us page only (for example).
I`ve seen in some tutorials that instead of 'page' you can use an ID or a slug of page, but that did not work for me.
Therefore, I really need your help/advice on that.
Thanks a lot in advance.
Try to do it like this:
<?php
function custom_meta_boxes(){
global $post;
$postSlug = isset($post->slug) ? $post->slug : '';
if ($post->slug === 'about-us') {
add_meta_box(
'custom_meta_1',
'About Us Main field',
'custom_meta_boxes_render',
'page',
'normal',
'high',
''
);
}
}
And change about-us slug for that you want to add meta box.

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Resources