remove custom meta boxes not working - wordpress

what I was trying to do here is to remove some custom fields that I created when a template is selected, aka when I select certain template I want to hide or show specific metaboxes.
The code I have is the following but it isn't working at all (thats to say that it doesn't remove any metaboxes) and I would like help to see what's wrong with it or if what I'm trying to do it's just not posible.
add_action('admin_init','my_meta_init');
function my_meta_init(){
$template_file = get_post_meta(get_the_ID(), '_wp_page_template', TRUE);
if (($template_file == 'thanks-template.php') || ($template_file == 'view-template.php'))
{
remove_meta_box('my_meta_box_id','page','normal');
remove_meta_box('my_meta_box_id_2','page','side');
remove_meta_box('my_meta_box_id_3','page','side');
remove_meta_box('dynamic_sectionid','page','normal');
} else
{
remove_meta_box('my_meta_box_id_4','page','normal');
}
}

Thanks you for the comments and answer, everyone helped. The problem was on the hook I was using, I changed it and now it's working perfectly :
add_action('admin_head','my_meta_init');

You may need to change the HOOK you are using to hook in your function.
That is you need to hook into admin_menu instead of admin_init as the metaboxes might not exist the moment you are trying to remove them. So a certain order is needed to make sure metaboxes removal call is made when actual metaboxes are generated and exist.
I tested following code on my localhost and it hid the Author div/metabox fine when used this code snippet:
function remove_page_fields() {
remove_meta_box( 'authordiv' , 'page' , 'normal' ); //removes author
}
add_action( 'admin_menu' , 'remove_page_fields' );
Another Approach:
By the way, as I think about the situation you are facing, maybe add the metaboxes/custom fields in such a way, that they are shown only to the pages we are meant to. I have worked on projects where I need to show some metaboxes only when certain template is selected.
I use CMB2 class to generate metaboxes mostly, if you happen to use that or something similar, you may use this parameter to specify the page templates https://github.com/WebDevStudios/CMB2/wiki/Display-Options#limit-to-specific-page-templates

Related

BuddyPress: Modify Name field template in Register form

The registration form in the Buddypress auto generates html for the dynamic fields (such as name). How can I modify the label text (for example replace "(required)" with an * (asterisk).
If you're looking to make that type of change to BuddyPress profile fields, you can use these filters:
add_filter( 'bp_get_the_profile_field_name', function ($field_name){
if($field_name === 'Name') {
return 'Your Name';
}
return $field_name;
});
add_filter( 'bp_get_the_profile_field_required_label', function($string, $id) {
return '*';
});
If you're looking to make changes to those account fields on the left, it looks like you'll need to use a template file copied from BuddyPress into your theme. A bit more intrusive, but will work.
Copy this: buddypress/bp-templates/bp-legacy/buddypress/members/register.php
To here: /your-theme/buddypress/members/register.php
Hope this helps a bit! Customizing BuddyPress & WordPress markup can get stringy real fast, so I'd recommend tracing the code back to a filter or action if you can find one. If not, look for a template file you can bring into your own theme.
If I did not undestand you wrong, It depends if you want to do it in the client (with Javascript for example) or from the server (with the back-end languaje). But the common languajes have a replace() method that takes two arguments, usually strings with the text you want to replace and the one you want to replace it with.
This page explains the replace method:
https://www.w3schools.com/jsref/jsref_replace.asp
Hope it help

Finding hooks Wordpress

I know there are lists of hooks for WordPress like --> http://adambrown.info/p/wp_hooks/hook
But if I want to find hooks for a plugin like WC Vendors there is a much shorter list of hooks on their website.
Are 'do_action' and 'apply filter' functions the only thing we can modify?
If given a class like --> https://github.com/wcvendors/wcvendors/blob/master/classes/admin/class-product-meta.php#L10, is there any way to modify it?
Are we limited to the do_action hooks or is there a way to modify other areas as well? Can we use the WordPress hooks to hook into the WC Vendors plugin as well?
Mostly you should try to accomplish any task with hooks, but some tasks are just not possible without actually modifying the actual code. But we all know its not good to modify core code, as all changes disappear after an update. So instead of modifying a class, you can extend it. You can override the current features and also add new ones. Extending a class is as easy as using a relavant hook in functions.php and then extending it in the same file or requiring it from another file. Here is an official tutorial on how to add a new shipping method to the woocommerce shipping class.
Sometimes you dont even need all the hooks, you just need to find the ones that are running on a specific page. For this you can use the code below to list all the current hooks.
$debug_tags = array();
add_action( 'all', function ( $tag ) {
global $debug_tags;
if ( in_array( $tag, $debug_tags ) ) {
return;
}
echo "<pre>" . $tag . "</pre>";
$debug_tags[] = $tag;
} );
Or you can use this plugin "simply show hooks"which is really helpful while development as it gives you an idea of where each hook is being triggered on the page.

Custom page classes not showing up in code

I tried adding page classes via Appearance => Menus => Screen Options => CSS Classes, but when I update my pages the classes don't show up.
I tried with another theme, it works, so my problem is theme-related. I didn't develop this theme, I took it back from another developer. Can someone explain me what I should look for in the code to make it back to normal?
Thank you for the help.
It could be a couple of things, where to look first.
Look for some standard functions are in the templates file such as body_class(), post_class(). As it print standard Wordpress used class within the context it needs to.
If its more menu related look for add_filters() within the functions.php filters that allow css to be added or removed such as.
add_filter('nav_menu_css_class' , 'some_function_called' , 10 , 2);
function some_function_called($classes, $item){
if(is_single() && $item->title == "Blog"){ //Notice you can change the conditional from is_single() and $item->title
$classes[] = "special-class";
}
return $classes;
}
Best of luck.

Customize search results for Custom Post Types

I am writing a Wordpress plug-in that creates several Custom Post Types (CPT). For they have their own custom fields, that need to be displayed in the search results, I need to customize the search results output.
Do I need to write my own theme for that or is there a hook (or other way) to solve this in my plug-in code?
You could hook into get_the_content and get_the_excerpt filters and test with is_search() to see if you should alter the returned value or not.
Not tested, but this is the idea:
add_filter( 'get_the_excerpt', 'my_search_excerpt' );
add_filter( 'get_the_content', 'my_search_excerpt' );
function my_search_excerpt( $content ) {
if ( is_search() ) {
$content = 'This is a search excerpt for ' . get_the_title();
// maybe add a read more link
// also, you can use global $post to access the current search result
}
return $content;
}
I see four possibilities:
Create a new (child) theme
Override the search template using filters (template_include)
Use client-side code to modify the appearance (CSS / JavaScript, poor workaround)
Hook the_content or the_excerpt
The easiest way might be to copy the search.php file of your installed theme and modify it to fulfill your needs. You can then hook it in using the first or second way. The first requires you to create a child theme, the second to create a plugin. The latter might be more complex so I would suggest to create a theme (take a look at template files of child themes for an explanation).

is it possible to make a custom page in buddypress

is it possible to make a custom page for buddypress with its url like this: http://domain.com/custom_page ? I found some answers by searching google but it does not create a custom page. i have a code here that i found in one of the blogs.
define('BP_FUN_SLUG','fun');
function bp_show_fun_page() {
global $bp, $current_blog;
if ( $bp->current_component == BP_FUN_SLUG && $bp->current_action == '' ) {
// The first variable here must match the name of your template file below
bp_core_load_template( 'fun', true );
}
}
add_action( 'wp', 'bp_show_fun_page', 2 );
but this code does not work... Is anyone there knows how to do this? thanks
Yes, it is possible to create a new page in Buddypress.
In Buddypress you have to create a new plugin or create a function in the theme functions file.
For creating first you have to add a new page link in navigation menu using bp_core_new_nav_item() function (You have created sub menu for that use bp_core_new_subnav_item() function).
Above two functions pass the screen function name as a parameter this name use when you click the custom page link call to this screen function. Create new function in your functions.php file same as screen function name. In this function call to your custom template file using bp_core_load_template() function.
Then finish, add more logic to create a new function and call it in the template file.
Another approach is to add a plugin that allows php in posts. For example http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Then create a page and add this to it:
[php] locate_template( array( 'some-template-folder/something.php' ), true ); [/php]
In case someone is wondering how to integrate custom pages into user profile (so that it looks like activity stream, groups etc.).
One thing i did recently was define a plugin (functions.php would work as well), register a custom slug with bp_core_new_nav_item or bp_core_nav_subnav_item and loaded member/single/plugins.php template in the handlers for those slugs. There are a bunch of actions on that page that you can hook into like bp_template_title and bp_template_content.
This can give you a whole lot of control.
Here's a link to the source of plugins.php: http://phpxref.ftwr.co.uk/buddypress/nav.html?readme.txt.source.html

Resources