WordPress - Dynamically add user ID to the end of a URL - php

I'm looking for a way to have the current logged in username added to the end of a link.
example: www.mywebsite.com/custom-post-type/author-post-title
"author-post-title" would be replaced with the current logged in username
This link will lead to a private post that is automatically created using Gravity Forms. The post title is the same as the logged-in person's username. I would like this link to be in place dynamically beforehand. Each author is limited to only one post and users already have to be logged in to see the link in nav menu.
I'm thinking there is a way to code this where the author name is a placeholder and is replaced by the currently logged in user ID, but I have not been able to figure it out. Something like... but this is way off as I am new to php.
<?php
function replace_text($text) {
$text = str_replace('author-post-title', '$user_id', $text);
$user_id = get_current_user_id();
return $text;
}
?>

I would recommend you to make generic page which has same URL for all users and modify it's content for specific user. It will be easier to attach this link to nav menu.
But, if you really need to create this kind of link you may create it using function:
function get_user_specific_url() {
if ( is_user_logged_in() ) {
return sprintf(
'%s/custom-post-type/%s',
get_site_url(),
wp_get_current_user()->user_nicename
);
}
// Redirect home in case if not logged in
return get_site_url();
}
Then you may want to create custom rewrite rule for this link structure to display specific page for user:
add_action( 'init', function() {
add_rewrite_tag("%user_nicename%", '([^/]*)');
add_rewrite_rule(
'^custom-post-type/([^/]*)/?',
'index.php?page_id=123&user_nicename=$matches[1]',
'top'
);
} );

You could try adding the following shortcode to your page:
[replace_link_w_user link="add your link here without username" button="Add button text here"]
Then add the following to your functions.php:
add_shortcode( 'replace_link_w_user', 'rl_w_user' );
function rl_w_user($atts = array()) {
$details = shortcode_atts( array(
"link"=>'',
"button_text"=>'Search'
), $atts );
if ( is_user_logged_in() ) {
$button='
<form action="'.$details['link'].'/'.wp_get_current_user()->user_nicename.'">
<input type="submit" value="'.$details['button_text'].'" />
</form>';
return $button;
} else {
return;
}
}

Related

Custom Endpoint Created, but I need to target a dynamic page that doesn't exist

I currently have a Code Snippet that's creating a custom tab through the WooCommerce Menu called 'Device Profile':
function AddCustomTabEndpoint() {
add_rewrite_endpoint('Device-Profile', EP_ROOT | EP_PAGES );
}
add_action('init','AddCustomTabEndpoint' );
function CustomTabQueryVars( $vars ) {
$vars[] = 'Device-Profile';
return $vars;
}
add_filter('query_vars','CustomTabQueryVars', 0 );
function AddCustomTabMyAccount( $items ) {
$items['Device-Profile'] = 'My Custom Tab';
return $items;
}
add_filter('woocommerce_account_menu_items','AddCustomTabMyAccount');
function MyCustomTabContent() {
echo "<h3>Device-Profile</h3><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>";
}
add_action('woocommerce_account_Device-Profile_endpoint','MyCustomTabContent');
..but now there is a brick wall. I can't point the menu link to a specific page because it doesn't exist. The page is created after a user submits a form which would look something like https://www.123.com/page_is_built/12345.
This URL doesn't lead anywhere, but as a reference, "page_is_built" would define the subdirectory and "12345" would be the endpoint that needs to be targeted. Again, this doesn't exist until the user submits a form on the website. I also can't predict what the endpoint URL will be... So how would I target the endpoint?

How can I edit this code so that it only changes the Top Menu bar on my homepage when a user is logged in without changing the Main Menu?

I am using OceanWP theme to create my site.
I have two different menus on my homepage, I have a top menu and I also have a main menu. http://prntscr.com/pofn5r
I would like for the top menu to display different options for users who are logged in and users who are logged out.
I have used the following code which I placed in the functions.php file. I have also created two different menus for logged in and logged out users:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged_in';
} else {
$args['menu'] = 'logged_out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
It seems to work in terms of showing different menus for users which are logged in and users which are logged out but the problem is that it also changes the main menu as well as the top menu.
prntscr.com/podv5e
I wanted the main menu to remain the same and just have the top menu change.
I was wandering whether there was a way I could adjust the code so that it would only apply to the top menu and not the main menu?
You could do something like:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
echo "Here you put HTML code when logged in";
} else {
echo "Here you put HTML code when logged out"
}
}
since echo will work like it is writted directly into HTML

Hide/replace menu item when user is logged in/out

I want to be able to hide or even replace the 'My account' button when users are logged out and I want to be able to hide or replace the 'Registration' button when users are logged in.
How would I go about doing this? I'm still a amateur at WordPress and I'm still learning, this is what I have so far in my nav-menus.php file.
if( is_user_logged_in() ) {
wp_nav_menu( array( 'My Account' => 'logged-users' ) );
} else {
wp_nav_menu( array( 'Registration' => 'not-logged-users' ) );
}
I know this isn't correct.
First, you need to create both menus, go to Appearance » Menus, create the two menus logged-in and logged-out.
After creating the menus, add this code in your theme’s functions.php file or a site-specific plugin:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-in'; //This value stands for the actual name you give to the menu when you create it.
} else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
}
That's all.
If you only need to hide them, then I would do this mainly via CSS. WP adds the class logged-in to the body element when there is a logged-in user, so you can use that to format elements inside of body differently.
Add classes to your menu items via the admin backend, like for example hide-when-logged-in and hide-when-not-logged-in.
Then you can use
body.logged-in .hide-when-logged-in,
body:not(logged-in) .hide-when-not-logged-in {
display: none;
}
in your stylesheet to hide those elements under the appropriate condition.

Redirect customers after leaving woocommerce product review

I am trying to redirect users to a thank you page after leaving a product review in woocommerce. I know the code
add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment($location)
{
return $_SERVER["HTTP_REFERER"];
}
will redirect all comments to a location, but I can't seem to find a filter or hook to specify only woocommerce product reviews. Has anyone done this before? Ideally I would just like to redirect to a standard wordpress page so I can add options and features to that pages template file in the future.
The comment_post_redirect filter has a second parameter. This parameter is the $comment object from which we can grab the post's ID. With the post's ID you can test for post_type and adjust the returned variable accordingly.
add_filter( 'comment_post_redirect', 'redirect_after_comment', 10, 2 );
function redirect_after_comment( $location, $comment ){
$post_id = $comment->comment_post_ID;
// product-only comment redirects
if( 'product' == get_post_type( $post_id ) ){
$location = 'http://www.woothemes.com';
}
return $location;
}

How to add custom bulk actions in WordPress list tables?

I am developing a plugin for that I have to black list users, so I need to be display one more dropdown item called Black List inside the Bulk Actions dropdown in the Users page, after the Delete option. But I'm unable to see from where these two actions are coming from and also how to black list a particular user.
My idea is to add one more field is_blacklisted in user table as Boolean with default value false and when apply Black List action it changes to true. Any other thoughts?
There's a filter, but it's only useful to remove bulk actions.
From this WPSE question, answer and comments, there's the following workaround: add a custom option to the dropdown with jQuery and hook into admin_action_$your-action to catch the submission.
The hook admin_footer-$current_page is used to print our JavaScript on a specific admin page (adjust to use in other screens).
add_action( 'admin_footer-users.php', 'bulk_footer_so_23541269' );
add_action( 'admin_action_black_list', 'bulk_request_so_23541269' );
function bulk_footer_so_23541269()
{
# global $typenow; if( $typenow != 'page' ) return; // if used on edit.php screen
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('<option>').val('black_list').text('Black list')
.appendTo("select[name='action'], select[name='action2']");
});
</script>
<?php
}
function bulk_request_so_23541269()
{
# Array with the selected User IDs
wp_die( '<pre>' . print_r( $_REQUEST['users'], true ) . '</pre>' );
// $_REQUEST['post'] if used on edit.php screen
}
Your doubt about blocking a user deserves another question, but I'd start a research here first.
Proper support with add_filter( 'bulk_actions-screenid', 'register_my_bulk_actions' ) is arriving in Wordpress 4.7 .
Quoting the announcement post:
To add an option in the Bulk Actions dropdown HTML element, register a callback on the bulk_actions-{screen_id} filter that adds the new option onto the array. Replace {screen_id} with the ID of the admin screen to offer the bulk action on.
To add a bulk action “Email to Eric,” we could use the following code:
add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );
function register_my_bulk_actions($bulk_actions)
{
$bulk_actions['email_to_eric'] = __( 'Email to Eric', 'email_to_eric');
return $bulk_actions;
}
To handle a bulk action form submission, register a callback on the handle_bulk_actions-{screen_id} filter for the corresponding screen. The filter expects the redirect URL to be modified, so be sure to modify the passed $redirect_url. This allows us to carry success or failure state into the next request to display a notice to the user. The other callback arguments will differ depending on the screen here to include contextually relevant data.
To add a bulk action handler for emailing the selected posts, we could use the following code:
add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 );
function my_bulk_action_handler( $redirect_to, $doaction, $post_ids )
{
if ( $doaction !== 'email_to_eric' ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
// Perform action for each post.
}
$redirect_to = add_query_arg( 'bulk_emailed_posts', count( $post_ids ), $redirect_to );
return $redirect_to;
}
Showing notices: We could use the existing notice hooks to let the user know what happened, depending on the state we set in the URL:
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );
function my_bulk_action_admin_notice()
{
if ( ! empty( $_REQUEST['bulk_emailed_posts'] ) ) {
$emailed_count = intval( $_REQUEST['bulk_emailed_posts'] );
printf( '<div id="message" class="updated fade">' .
_n( 'Emailed %s post to Eric.',
'Emailed %s posts to Eric.',
$emailed_count,
'email_to_eric'
) . '</div>', $emailed_count );
}
}

Categories