🎉 Happy 4th of July! Get 15% off GP Premium with the discount code: 4THOFJULY2020

[Resolved] Adding shortcode to php

Home Forums Support Adding shortcode to php

  • This topic has 13 replies, 5 voices, and was last updated 4 months ago by Tom.
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1159109
    Carlo

    I’d like to add the following shortcode to the below snippet code but not quite sure how to achieve this:
    [editorskit display=”wordcount” before=”Reading Time: ” after=” min”]

    Im currently using this php snippet code to display metadata under my headings

    add_filter( 'generate_post_author', '__return_false' );
    add_filter( 'generate_show_comments', '__return_false' );
    
    add_filter( 'generate_post_date_output', function( $date, $time_string ) {
    printf( ' <span class="byline">%1$s</span>',
    sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
    esc_html( get_the_author() ),
    get_avatar( get_the_author_meta( 'ID' ) )
    )
    );
    
    if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
    echo '<span class="comments-link">';
    comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
    echo '</span>';
    }
    
    printf( '<span class="posted-on">%s</span>',
    $time_string
    );
    }, 10, 2 );

    and styled with the following CSS:

    .byline img {
        width: 25px;
        height: 25px;
        border-radius: 50%;
        position: relative;
        vertical-align: middle;
        margin: 0 10px 0 0;
    }
    
    .byline,
    .comments-link,
    .posted-on {
    	display: inline-block;
    }
    
    .comments-link,
    .posted-on {
    	border-left: 1px solid #ddd;
    	padding-left: 10px;
    	margin-left: 10px;
    }
    
    .comments-link:before {
    	display: none;
    }
    
    h2.entry-title {
        margin-bottom: 20px;
    }
    #1159141
    Leo
    Staff
    Customer Support
    #1159321
    Carlo

    thanks Leo, yeah i did some searching and did see that code mentioned before, my problem is im not sure how to incorporate it into the code im using above. I keep getting errors when i add it so i guess im adding it in the wrong place?

    <?php echo do_shortcode("[Shortcode]"); ?>

    #1159333
    Carlo

    Just some context in what the layout im trying to achieve:

    HEADING
    (author image) | author name | publish date | (shortcode = reading time)

    anyway to add on the shortcode to the end of the entry meta?

    #1159780
    Tom
    Lead Developer
    Lead Developer

    Try this:

    echo do_shortcode( '[editorskit display="wordcount" before="Reading Time: " after=" min"]' );

    Let me know 🙂

    #1161393
    Carlo

    Got it working, thanks Tom.

    just one more thing, ive been playing around with the CSS to get them to stack nicely under each other in mobile view but just doesn’t look right.

    Im trying to achieve the following layout in mobile. (with the solid border removed on .posted-on class. any quick tips here?

    (author image) & author name
    publish date
    [shortcode = reading time]

    #1162110
    Tom
    Lead Developer
    Lead Developer

    Any chance you can link me to the page so I can inspect the CSS/HTML?

    #1166755
    Carlo

    Hi Tom, I updated the original post to include a link to the post.
    i’ve added it to a test site until i work it out so i dont mess up my working site.

    also, ive added the “reading time shortcode” towards the bottom of this code, is this the right place to add it?

    add_filter( 'generate_post_author', '__return_false' );
    add_filter( 'generate_show_comments', '__return_false' );
    
    add_filter( 'generate_post_date_output', function( $date, $time_string ) {
    printf( ' <span class="byline">%1$s</span>',
    sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%4$s<a href="%1$s" title="%2$s" rel="author"><span class="author-name" itemprop="name">%3$s</span></a></span>',
    esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
    esc_html( get_the_author() ),
    get_avatar( get_the_author_meta( 'ID' ) )
    )
    );
    
    if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
    echo '<span class="comments-link">';
    comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
    echo '</span>';
    }
    
    printf( '<span class="posted-on">%s</span>',
    $time_string
    );
    echo do_shortcode( '[editorskit display="wordcount" before="Reading Time: " after=" min"]' );
    }, 10, 2 );
    #1166854
    David
    Staff
    Customer Support

    Hi there,

    try this CSS for your mobile arrangement:

    @media (max-width: 768px) {
        .entry-meta .posted-on,
        .editorskit-shortcode {
            display: block;
            border-left: 0;
            padding-left: 0;
            margin-left: 0;
        }
    }

    And where you added the Shortcode looks fine.

    #1173852
    Carlo

    Thanks David, worked like a charm

    #1175099
    David
    Staff
    Customer Support
    #1187773
    Bas

    Hi, sorry to break into this thread but I have the same problem.
    I can’t get the syntax right.
    As of now, the second shortcode gets added into the first list item.

    What am I doing wrong?

    
    add_filter( 'generate_post_author_output', 'add_to_end_of_meta' );
    function add_to_end_of_meta( $output ) {
      
      echo $output . ' <ul class="reads-shares-meta">
    	             <li class="post-reads-info">' . do_shortcode('[post-views]'); '</li>
                         <li class="post-shares-info">' . do_shortcode('[social_warfare buttons="totals"]'); '</li>
                       </ul> '; 
    }
    
    #1187971
    Bas

    And as always…. minutes after posting a question I find a working solution lol

    
    add_filter( 'generate_post_author_output', 'add_to_end_of_meta' );
    function add_to_end_of_meta( $output ) {
      
      echo $output . ' <ul class="reads-shares-meta">
    	                 <li class="post-reads-info">' . do_shortcode("[post-views]") . '</li>
                         <li class="post-shares-info">' . do_shortcode('[social_warfare buttons="totals"]') . '</li>
                       </ul> '; 
    }
    
    #1188022
    Tom
    Lead Developer
    Lead Developer
Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.