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

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

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1362188
    Prakhar

    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.

    #1362269
    David
    Staff
    Customer Support

    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.

    #1362666
    Prakhar

    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.

    #1362668
    Prakhar

    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.

    #1362824
    David
    Staff
    Customer Support

    You could use the author ID for the condtion eg.

    // Get author ID outside of loop
    global $post;
    $author_id = $post->post_author;
    
    if ( 10 == $author_id ) {
        // do your stuff for Author ID 10
    }

    See here:
    https://developer.wordpress.org/reference/functions/get_the_author_meta/#comment-3500

    #1362871
    Prakhar

    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.

    #1362874
    Prakhar

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

    #1362878
    David
    Staff
    Customer Support

    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.

    #1362888
    Prakhar

    Thanks Sir.

    #1362985
    David
    Staff
    Customer Support
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.