Skip Navigation

[Resolved] Custom Hook with CPT

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by geoffD 4 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1386417

Hello!

I have created a custom hook in the functions.php file to return a permalink for the current logged in users 1st created CPT 'User Will'

$ushub = array('post_type' => 'user-will',
              'fields' => 'ids',
            'meta_query' => array(
            array(
                'key' => 'wpcf-user-id',
                'value' => $current_user->ID
             ),
        ),
     );
  
     $user_wills= get_posts($ushub);
	 $user_will_url=get_permalink($user_wills[0]);

This works perfectly in all of my situations including creating a custom menu link as below:

function custom_add_userhub_link( $items, $args ) {
	global $user_will_url;
    if ($args->theme_location == 'primary'){
        if (is_user_logged_in()) {
            $items .= '<li><a href="' . $user_will_url . '">User Hub</a></li>';
        } else {
            $items .= '<li><a href="<em><u>hidden link</u></em>">Log In</a></li>';
}
    }

    return $items;
}

add_filter( 'wp_nav_menu_items', 'custom_add_userhub_link', 10, 2 );

And many other similar functions....

MY PROBLEM.....
I have another CPT called 'Partner Email' and when a user creates a 'Partner Email', the $user_will_url changes from:
hidden link
to:
hidden link

I just can't get to the bottom of this problem..please can you help...

Very best regards

Geoff

#1386691

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As you are not sure yet, I'm not sure where the URL rewrite take place when you create Partner Email.

- Are you using any URL rewrite plugin?
- Are you using any custom rewrite rules with your htaccess file or any plugin?
- Have you set any custom rewrite URL to your post types?
- Is this only happen when you create a partner email new entry?

In order to rule out any possible compatibility issue:
Could you please try to resolve your issue by deactivating all third-party plugins as well as the default theme to check for any possible conflicts with any of the plugins or themes?

Do you see any difference?

#1386747

Hello Minesh..

Thank you as always for your support ..it's always very much appreciated..

In relation to your questions:
- Are you using any URL rewrite plugin?
No
- Are you using any custom rewrite rules with your htaccess file or any plugin?
No
- Have you set any custom rewrite URL to your post types?
No but as mentioned above I use the below hook to return the users 1st 'user-will' CPT

$ushub = array('post_type' => 'user-will',
              'fields' => 'ids',
            'meta_query' => array(
            array(
                'key' => 'wpcf-user-id',
                'value' => $current_user->ID
             ),
        ),
     );
  
     $user_wills= get_posts($ushub);
	 $user_will_url=get_permalink($user_wills[0]);

- Is this only happen when you create a partner email new entry?
Yes when I create every other CPT it's fine but when I go to the page which has a cred form to create a new CPT 'partner-email' this is when it happens...

I have tried disabling theme and plugins but the problem persists ..

Many thanks

Geoff

#1386779

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

with what hook you wrapped the code you shared in your previous post?

$ushub = array('post_type' => 'user-will',
              'fields' => 'ids',
            'meta_query' => array(
            array(
                'key' => 'wpcf-user-id',
                'value' => $current_user->ID
             ),
        ),
     );
   
     $user_wills= get_posts($ushub);
     $user_will_url=get_permalink($user_wills[0]);

- Is this only happen when you create a partner email new entry?
Yes when I create every other CPT it's fine but when I go to the page which has a cred form to create a new CPT 'partner-email' this is when it happens...
===>
Where did you add the toolset form? Can you please share the link where form is added?

Can I have access details so I can check this further?

*** Please make a FULL BACKUP of your database and website.***
I have set the next reply to private which means only you and I have access to it.

#1386965

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As the code resides in the functions.php file I need FTP access details as well so that I can modify it and adjust it.

Can you please send me FTP access details as well.

I have set the next reply to private which means only you and I have access to it.

#1387779

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing FTP access details. FTP access details are working but I do not see any files/folders once I log in to FTP. Maybe permissions are missing. Can you please grant permissions so that I can access the files using FTP.

I have set the next reply to private which means only you and I have access to it.

#1387853

Hi Minesh

Apologies..I have set it up correctly now to the right path...If you could try again with the same details please ..

Best regards

Geoff

#1387951

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Actually, you have used the code you shared wrongly.

$ushub = array('post_type' => 'user-will',
              'fields' => 'ids',
            'meta_query' => array(
            array(
                'key' => 'wpcf-user-id',
                'value' => $current_user->ID
             ),
        ),
     );
   
     $user_wills= get_posts($ushub);
     $user_will_url=get_permalink($user_wills[0]);

I've wrapped the above code with the function as given under:

/*  Return User Will URL */
function get_current_will_url(){
global $current_user;

$ushub = array('post_type' => 'user-will',
              'fields' => 'ids',
            'meta_query' => array(
            array(
                'key' => 'wpcf-user-id',
                'value' => $current_user->ID
             ),
        ),
     );
  
     $user_wills= get_posts($ushub);
	 $user_will_url=get_permalink($user_wills[0]);
	 return $user_will_url;
}

Now, whereever you are trying to use the $user_will_url, you should change that to: get_current_will_url();

so, I've changed the wp_nav_menu_items hook as given under:

/* Add User Hub Menu Item */
function custom_add_userhub_link( $items, $args ) {
	global $user_will_url;
    if ($args->theme_location == 'primary'){
        if (is_user_logged_in()) {
			 
          //  $items .= '<li><a href="' . $user_will_url . '">User Hub</a></li>';
			  $items .= '<li><a href="' . get_current_will_url() . '">User Hub</a></li>';
        } else {
            $items .= '<li><a href="<em><u>hidden link</u></em>">Log In</a></li>';
}
    }

    return $items;
}

add_filter( 'wp_nav_menu_items', 'custom_add_userhub_link', 10, 2 );

Now, when I submit the new entry using the form: hidden link

I see the URL stays at: hidden link

Can you please confirm it works at your end as well.

#1387957

Minesh you are a wizard!...Thank you as always... I have a couple of other hooks that use the use the $user_will_url e.g.

/* Redirect to User Hub if About Yourself section complete */
add_filter('cred_success_redirect', 'section_status_complete_2',10,3);
function section_status_complete_2($url, $post_id, $form_data){	
global $user_will_url;	
    if ($form_data['id']==3117)
        return $user_will_url;
    return $url;
   }
/* END - Redirect to User Hub if About Yourself section complete */

Would I just replace $user_will_url with get_current_will_url(); ?

Also I have set up a short code for use in the pages where I want the user to redirect to their user-hub...Here is the code...do I replace the same as above?

/* Create Shortcode for User Hub Link */
add_shortcode( 'uhlink', 'ul_func' );
function ul_func() {
	global $user_will_url;
    return  $user_will_url;
}
/* /Create Shortcode for User Hub Link */

I will do some testing also and let you know of any other issues..

Thanks again

Best regards

Geoff

#1387961

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you, your functions.php file was really heavy, full of code but I'm glad that the solution I shared helps you to resolve your issue.

Yes - it should be changed to the function I shared.

/* Redirect to User Hub if About Yourself section complete */
add_filter('cred_success_redirect', 'section_status_complete_2',10,3);
function section_status_complete_2($url, $post_id, $form_data){ 
global $user_will_url;  
    if ($form_data['id']==3117)
        return get_current_will_url();
    return $url;
   }
/* END - Redirect to User Hub if About Yourself section complete */
/* Create Shortcode for User Hub Link */
add_shortcode( 'uhlink', 'ul_func' );
function ul_func() {
    global $user_will_url;
    return   get_current_will_url();
}
/* /Create Shortcode for User Hub Link */

Have a good day 🙂

#1388679

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please feel free to mark resolve this ticket 🙂

#1388685

My issue is resolved now. Thank you!