Site logo

Archived topic

How can i add php function only for specific user/author in wordpress

9 replies · Started by Prakhar on July 13, 2020

Viewing posts 1–10 of 10

Hi, i am currently using generatepress and wanna add some php snippet for authors on frontend. so how can i add different php snippet for different users/authors.

Hi there,

It depends on what you're trying to do.
For example - show content based on User Role or Log-in status you can simply use the Hook Element, and select the User location in Display Rules.

If you wanted to display content thats related to the current post author you could use the get_author_meta function to write your own conditionals.

https://developer.wordpress.org/reference/functions/get_the_author_meta/

If you can provide an example of what you're trying to do - we can try and be more specific.

We are trying to add social media icons by php snippet but it was showing same for all authors. so how we can add different icons for all authors by using if or anything else. note that we are not using wordpress author meta social links, we are using custom social icons which include more social media links than wordpress default means custom php snippet.

example- if we wanna add php snippet only for home then we use if( (is_home)). so how we can use this type of condition in above matter.

Thanks, But How To Find Id Of Admin Who Is Also A Author On That Site. I Am Unable To Find Admin's ID As A Author.

unable to find main admin's id how we can apply above conditional code for it

As per the article i linked to - you can use the ID to get the nicename for example. then you can use that as your condition:

// Get author ID outside of loop
global $post;
$author_id = $post->post_author;
// Get author nicename
$author_nicename = get_the_author_meta( 'nicename', $author_id );

if ( 'Fred' == $author_nicename ) {
    // do your stuff for Fred
}

If you have a lots of authors, and each of them have their own social links, then i would recommend using an Author Box plugin that supports social links.

Thanks Sir.

You're welcome

This archived topic is closed to new replies.