WordPress.org

Ready to get started?Download WordPress

Forums

php inserting code into shortcode help (5 posts)

  1. posthawk
    Member
    Posted 1 year ago #

    What i'm trying to make is a widget that will display a twitter widget through shortcode, but it will automatically insert the author of the post's twitter id as the username. The id is gotten through the users profile.

    <?php
    //this retrieves the authors twitter username
    $tweetid = get_the_author_meta( 'twitter' );
    //this should enter that username unto the required field of the shortcode
    $tweetcode = '[rotatingtweets screen_name="$tweetid"]';
    //this tells the widget not to display if there is no twitter data filled out
    $tweetcheck = get_the_author_meta( 'twitter' );
    
    if ($tweetcheck)  { ?
    
    //this should run the shortcode with the propper field filled out
    
    <?php
     echo do_shortcode($tweetcode);
    ?>
    
    <?php } ?>

    [Please use the code buttons OR backticks (not blockquote) for posting code - as is, it's likely your code has been corrupted here. http://codex.wordpress.org/Forum_Welcome#Posting_Code ]
    The problem i am having is that no matter what i do, the widget wont stop thinking that the screen_name is $tweetid and i keep getting a message to check the user name.

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    try to change the one line to:

    $tweetcode = '[rotatingtweets screen_name="'.$tweetid.'"]';

    there might also be an issue with wrongly placed/opened/not closed php tags (?);
    to clarify, please repost the code.

  3. Martin Tod
    Member
    Posted 1 year ago #

    Try this instead:

    //this retrieves the author's twitter username
    $tweetid = get_the_author_meta( 'twitter' );
    // Check that rotating tweets is installed and that there is a twitter id
    if (!empty($tweetid) && function_exists('rotatingtweets_display') )  {
        rotatingtweets_display( array (
            'screen_name' => $tweetid
        ));
    }
  4. Martin Tod
    Member
    Posted 1 year ago #

    Good point re: inverted commas. I guess changing to:

    $tweetcode = "[rotatingtweets screen_name='$tweetid']";

    should work too!

  5. Martin Tod
    Member
    Posted 1 year ago #

    Although I've just done some checking - and you do need to check the function is there.

    So something like:

    // this retrieves the author's twitter username
    $tweetid = get_the_author_meta( 'twitter' );
    //this should enter that username unto the required field of the shortcode
    $tweetcode = "[rotatingtweets screen_name='$tweetid']";
    // Check that rotating tweets is installed and that there is a twitter id
    if (!empty($tweetid) && function_exists('rotatingtweets_display_shortcode') )  {
        echo do_shortcode($tweetcode);
    }

    should also work

Topic Closed

This topic has been closed to new replies.

About this Topic