Skip to content

Instantly share code, notes, and snippets.

@pixelbart
Created August 13, 2020 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbart/0f4283baca2a7dbd75a8d4aac4f10b70 to your computer and use it in GitHub Desktop.
Save pixelbart/0f4283baca2a7dbd75a8d4aac4f10b70 to your computer and use it in GitHub Desktop.
Using Helpful WordPress Plugin on archive pages

Using Helpful on archive pages

With these two scripts Helpful can be used as short code on archive pages. Place the code in your functions.php to make it work.

In the first script the_content is manipulated and in the second script the_excerpt. Use what you have in use.

Quick tip: Use get_post_type( $post ) to check if a particular post type is in use.

<?php // functions.php
/**
* Includes Helpful on archive pages if Helpful is not yet included. Uses the_content() filter of WordPress for this.
*
* @param string $content
*
* @return string
*/
add_filter( 'the_content', function( $content ) {
global $post;
if ( ! preg_match( '~[helpful~', $content ) && is_archive() ) {
$content .= do_shortcode( sprintf( '[helpful post_id="%d"]', intval( $post->ID ) ) );
}
return $content;
});
<?php // functions.php
/**
* Includes Helpful on archive pages if Helpful is not yet included. Uses the_excerpt() filter of WordPress for this.
*
* @param string $excerpt
* @param WP_Post $post
*
* @return string
*/
add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
if ( ! preg_match( '~[helpful~', $content ) && is_archive() ) {
$content .= do_shortcode( sprintf( '[helpful post_id="%d"]', intval( $post->ID ) ) );
}
return $excerpt;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment