How to Change Wordpress Admin Bar My Account Profile URL - wordpress

My wordpress admin bar 'My Account' links takes the user to the back end to manage their profile etc. There are also buddypress links on that menu which take the user to manager their profile on the front end. I like the avatar with the 'Howdy' but I don't want users on the back
end; so I need to change the WP links to take them to the front end.
I found how to remove links and add links to the admin bar 'My Account' but I just want to change the url of those top links from /wp-admin/profile to /members/user/profile.
admin-bar.php
/**
* Add the "My Account" item.
*
* #since 3.3.0
*
* #param WP_Admin_Bar $wp_admin_bar
*/
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
Would changing the 'get_edit_profile_url' from a wordpress admin url to the buddypress members url be the simplest way to accomplish my goal? ... and How would I do that to test it?
Thanks to anybody who can help me, I'm not very good with php.

I couldn't find any help with changing the url however this redirect in my functions file keeps the user on the front end.
Thanks to: http://blog.happyplugins.com/redirect-user-profile-page-frontend-page/
add_action ('init' , 'prevent_profile_access');
function prevent_profile_access() {
if (current_user_can('manage_options')) return '';
if (strpos ($_SERVER ['REQUEST_URI'] , 'wp-admin/profile.php' )){
wp_redirect ('/members/' .bp_core_get_username(bp_loggedin_user_id() ));
die();
}
}
I'd still like to know how to change the URL instead of using the redirect, if anyone knows how!

bp_core_get_userlink( $user_id )

add_action('wp_before_admin_bar_render', 'rew_admin_bar_remove_wp_profile', 0);
function rew_admin_bar_remove_wp_profile() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('edit-profile');
}
add_action('admin_bar_menu', 'rew_add_bbp_profile', 999);
function rew_add_bbp_profile($wp_admin_bar) {
$current_user = wp_get_current_user() ;
$user=$current_user->user_nicename ;
$user_slug = get_option( '_bbp_user_slug' ) ;
if (get_option( '_bbp_include_root' ) == true ) {
$forum_slug = get_option( '_bbp_root_slug' ) ;
$slug = $forum_slug.'/'.$user_slug.'/' ;
}
else {
$slug=$user_slug . '/' ;
}
$profilelink = '/' .$slug. $user . '/edit' ;
$wp_admin_bar->add_node( array(
'parent' => 'user-actions',
'id' => 'bbp-edit-profile',
'title' => 'Edit Profile',
'href' => $profilelink,
) );
}
get you to the bbpress user profile

Related

how to change edit url with "Id" to edit url with "Slug" in Wordpress

I have this is WordPress Post editing Url:
https://example.com/wp-admin/post.php?post=ID&action=edit
and I want to change it to Slug Not the ID Like This:
https://example.com/wp-admin/post.php?post=Slug&action=edit
I was trying to edit the post with this Url but is not working:
https://example.com/wp-admin/post.php?post=MyPostSlug&action=edit
In order to change the edit post link structure, you can use the get_edit_post_link filter like so:
add_filter( 'get_edit_post_link', 'so_73914075_get_edit_post_link', 10, 3);
function so_73914075_get_edit_post_link($link, $post_id, $context) {
$post = get_post( $post_id );
if ( ! in_array( $post->post_type, array( 'post', 'page' ) ) ) {
return $link;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( 'revision' === $post->post_type ) {
$action = '';
} elseif ( 'display' === $context ) {
$action = '&action=edit';
} else {
$action = '&action=edit';
}
if ( 'display' === $context ) {
$post_type = '&post-type=' . $post->post_type;
} else {
$post_type = '&post-type=' . $post->post_type;
}
$custom_edit_link = str_replace( '?post=%d', '?post-name=%s', $post_type_object->_edit_link );
return admin_url( sprintf( $custom_edit_link . $action . $post_type, $post->post_name ) );
}
This will change the edit links for regular posts and pages to something like this:
https://example.com/wp-admin/post.php?post-name=the-post-slug&action=edit&post-type=post
WARNING: make sure you limit this to only the post types you need to change the URL for. Making this change global will almost surely
have unintended effects over other plugins
However, making the admin panel actually load the edit screen is not that easy.
Looking at the source code of the wp-admin/post.php file, you will see that there is no way to hook into the flow and populate the $post_id variable with the post id matching the slug you are sending through.
That means you have 2 options:
RECOMMENDED Update the edit link to whatever format you want and then create an admin redirect function that pulls the slug from the initial URL and redirects to the actual edit page with the ?post=%d in it.
NOT RECOMMENDED Create a new admin edit page that will understand your URL structure and include the wp-admin/post.php after the code that pulls the $post_id based on the slug.
The 2nd method might come with lots of extra code you need to write to cover all the cases and all the instances a post reaches the post.php in the admin panel

Change the default Buddypress user landing page differently depending on the type of user

I made a custom navigation page for users, using the plugins.php file.
But I would like this page/option to be only available for some types of users; and make it the default landing page for them.
I can't figure out how to that.
I tried to make it the default landing page, and in the plugins template file, adding a condition that redirect users depending on their type... but redirection doesn't work by there it seems.
Any clue, plz?
I'm using Wordpress 5.8.2 and Buddypress 9.1.1.
Thanks
I found a solution.
First, here's how I created a new navigation item; I put this code in my bp-custom.php file:
function bp_custom_user_nav_item() {
global $bp;
$args = array(
'name' => __('newnavitem', 'buddypress'),
'slug' => 'newnavitem',
'default_subnav_slug' => 'newnavitem',
'position' => 0,
'show_for_displayed_user' => true,
'screen_function' => 'bp_custom_user_nav_item_screen',
'item_css_id' => 'newnavitem'
);
bp_core_new_nav_item( $args );
}
add_action( 'bp_setup_nav', 'bp_custom_user_nav_item', 99 );
After I created a new directory in community/members/single in my theme directory, and in this directory I will put my newnavitem loop and edit template files.
And I linked the newnavitem and its template with this function
function bp_custom_user_nav_item_screen() {
add_action( 'bp_template_content', 'bp_custom_screen_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/newnavitem' ) );
}
And here's how I disabled the current nav item for certain user types, and set it as default landing page for another certain user types.
function conditionally_disable_newnavitem( $enabled, $component) {
if (user_can(bp_displayed_user_id(),'firstusertype') && $component === 'newnavitem') {
$enabled = false;
}
return $enabled;
}
add_filter( 'bp_is_active', 'conditionally_disable_new_navitem', 10, 2 );
function set_default_component () {
if ( user_can(bp_displayed_user_id(),'secondusertype') || user_can(bp_displayed_user_id(),'thirdusertype')) {
define ( 'BP_DEFAULT_COMPONENT', 'newnavitem' );
add_filter( 'bp_is_active', function($retval, $component){
if($component === 'newnavitem') return true;
return $retval;
}, 10, 2 );
} else {
define ( 'BP_DEFAULT_COMPONENT', 'activity' );
}
}
add_action( 'bp_core_setup_globals', 'set_default_component' );

How Can I Change the Elementor Popup Location Settings?

I have an issue with one of the websites I work on where the "edit_in_content" location setting for Elementor popups is set to FALSE for all popups, and it is preventing the admin from going to the Elementor editor.
This issue presents itself as the common "the_content not found" error message, however, debugging leads to the location settings for popups being the issue. The problem is in the builder_wrapper method of \ElementorPro\Modules\ThemeBuilder\Classes\Locations_Manager, which is in /elementor-pro/modules/theme-builder/classes/locations-manager.php.
public function builder_wrapper( $content ) {
$post_id = get_the_ID();
if ( $post_id ) {
$document = Module::instance()->get_document( $post_id );
if ( $document ) {
$document_location = $document->get_location();
$location_settings = $this->get_location( $document_location );
/**
* Custom Modification Begin
* ------------------------------------
*/
if( $document_location == 'popup' )
return $content;
/**
* Custom Modification End
* ------------------------------------
*/
// If is a `content` document or the theme is not support the document location (header/footer and etc.).
if ( $location_settings && ! $location_settings['edit_in_content'] ) {
$content = '<div class="elementor-theme-builder-content-area">' . __( 'Content Area', 'elementor-pro' ) . '</div>';
}
}
}
return $content;
}
I'm aware that this is a plugin hack, and not recommended, but I have found no other way to handle the issue.
I'm not sure why edit_in_content is FALSE for all popups.
I've found no way to ensure that edit_in_content is TRUE so that the admin can work with popups.
To be fair, I should disclose that the theme is custom, but rather minimal. When switching to twentytwenty the issue goes away. ALL plugins have been disabled during testing, and it seems that the popup location setting is somehow being affected by the custom theme.
I've found nothing in twentytwenty that references elementor in any way.
I've been through Elementor's docs related to the_content issues, and nothing noted there leads to success. Ref: https://docs.elementor.com/article/56-content-area-not-found
So, I'm hoping somebody will be able to shed some light on this issue. What can I do so I don't have to hack the plugin?
I'm posting an answer because I found something that I could do to not hack the plugin, although I'm still curious to why it needs to be done. I basically just add a hook/action that reprocesses the popup location after it was initially registered. This could go in the theme's functions.php file:
function fixElementorPopupLocation( $that )
{
$loc = $that->get_location('popup');
if( ! $loc['edit_in_content'] )
{
$args = [
'label' => $loc['label'],
'multiple' => $loc['multiple'],
'public' => $loc['public'],
'edit_in_content' => TRUE,
'hook' => $loc['hook'],
];
$that->register_location('popup', $args);
}
}
add_action(
'elementor/theme/register_locations',
'fixElementorPopupLocation',
93062220
);
If anyone could tell me why I need to do this, and why I don't need this in the twentytwenty theme, I'd be glad to accept that as an answer, provided I can use the advice to "fix" my theme.

wordpress - Change page name when user is logged out

I have a page named My account, I want when I log out to change the title of the page in "Log In / Register".
I tryed this code:
function wp_change_title( $title ) {
if ( !is_user_logged_in() && get_page_by_title( 'My Account' ) ) {
return 'Log In / Register';
}
return $title;
}
add_filter( 'the_title', 'wp_change_title' );
But it changes the name to all the pages when I log out, how do I do to only change the name of the My account page?
try is_page('My Account') instead of get_page_by_title(). get_page_by_title() is a query, not a conditional.

hide a woocommerce setting tab

I would like to hide a specific woocommerce setting tab by user role. Not the entire submenu, but just a tab(checkout to be specific).
I want shop managers to be able to access most of the settings, but be unable to affect the checkout settings.
How can I achieve this?
If you want to remove the tabs instead of hiding them using CSS, then you can add the following to yours theme functions.php:
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $tabs ) {
// Declare the tabs we want to hide
$tabs_to_hide = array(
'Tax',
'Checkout',
'Emails',
'API',
'Accounts',
);
// Get the current user
$user = wp_get_current_user();
// Check if user is a shop-manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Remove the tabs we want to hide
$tabs = array_diff($tabs, $tabs_to_hide);
}
return $tabs;
}
This uses the WooCommerce 'woocommerce_settings_tabs_array' filter. For more information on all the WooCommerce filters and hooks you can look here: https://docs.woocommerce.com/wc-apidocs/hook-docs.html
This just has the added benefit that it is no longer in the HTML, so if anyone looks at the source, they won't find the elements.
You can still access the URLs. This is just a way of removing the tabs instead of hiding them.
EDIT:
I've figured out how to stop access to the URLs. Copy the following:
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
// Declare the tabs we want to hide
$tabs_to_hide = array(
'tax' => 'Tax',
'checkout' => 'Checkout',
'email' => 'Emails',
'api' => 'API',
'account' => 'Accounts',
);
// Get the current user
$user = wp_get_current_user();
// Check if user is a shop_manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
// Remove the tabs we want to hide from the array
$array = array_diff_key($array, $tabs_to_hide);
// Loop through the tabs we want to remove and hook into their settings action
foreach($tabs_to_hide as $tabs => $tab_title) {
add_action( 'woocommerce_settings_' . $tabs , 'redirect_from_tab_page');
}
}
return $array;
}
function redirect_from_tab_page() {
// Get the Admin URL and then redirect to it
$admin_url = get_admin_url();
wp_redirect($admin_url);
exit;
}
This is pretty much the same as the first bit of code, apart from the array is structured differently and I've added a foreach. The foreach goes through the list of tabs we want to block, hooks into the 'woocommerce_settings_{$tab}' action which is used to show the settings pages.
Then I created a redirect_from_tab_page function to redirect the users to the default admin URL. This stops direct access to the different settings tabs.
Put this code in your theme/child theme functions.php or somewhere else:
if (!function_exists('hide_setting_checkout_for_shop_manager')){
function hide_setting_checkout_for_shop_manager() {
$user = wp_get_current_user();
//check if user is shop_manager
if ( isset( $user->roles[0] ) && $user->roles[0] == 'shop_manager' ) {
echo '<style> .woocommerce_page_wc-settings form .woo-nav-tab-wrapper a[href="'.admin_url('admin.php?page=wc-settings&tab=checkout').'"]{ display: none; } </style>';
}
}
}
add_action('admin_head', 'hide_setting_checkout_for_shop_manager');
The style will be output to html head only at wp-admin and the login user role is shop_manager.
For more about admin_head hook, please check https://codex.wordpress.org/Plugin_API/Action_Reference/admin_head

Resources