Created
November 14, 2011 16:17
-
-
Save eteubert/1364325 to your computer and use it in GitHub Desktop.
WordPress: Implement a Content Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class content_parser { | |
| public function __construct() { | |
| // apply parser to all posts | |
| add_filter( 'the_content', array( __CLASS__, 'parse' ) ); | |
| } | |
| public static function parse( $text ) { | |
| global $post; | |
| $text = str_replace( '%DATE%', get_the_date(), $text ); | |
| $text = str_replace( '%TITLE%', get_the_title(), $text ); | |
| $text = str_replace( '%AUTHOR%', get_the_author(), $text ); | |
| $text = str_replace( '%TAGS%', get_the_tag_list(), $text ); | |
| $text = str_replace( '%PERMALINK%', get_permalink(), $text ); | |
| $text = str_replace( '%COMMENTS%', get_comments_number(), $text ); | |
| $text = str_replace( '%CATEGORIES%', get_the_category_list(), $text ); | |
| // tags with custom separator | |
| $text = preg_replace_callback( | |
| '/%TAGS\|(.*)%/', | |
| function ( $matches ) { | |
| return get_the_tag_list( "", $matches[ 1 ], "" ); | |
| }, | |
| $text | |
| ); | |
| // categories with custom separator | |
| $text = preg_replace_callback( | |
| '/%CATEGORIES\|(.*)%/', | |
| function ( $matches ) { | |
| return get_the_category_list( $matches[ 1 ] ); | |
| }, | |
| $text | |
| ); | |
| // custom post meta | |
| $text = preg_replace_callback( | |
| '/%POST_META\|(.*)%/', | |
| function ( $matches ) { | |
| global $post; | |
| return get_post_meta( $post->ID, $matches[ 1 ], true ); | |
| }, | |
| $text | |
| ); | |
| // custom date format | |
| $text = preg_replace_callback( | |
| '/%DATE\|(.*)%/', | |
| function ( $matches ) { | |
| return get_the_date( $matches[ 1 ] ); | |
| }, | |
| $text | |
| ); | |
| // custom post thumbnails | |
| $text = preg_replace_callback( | |
| '/%POST_THUMBNAIL\|(\d+)x(\d+)%/', | |
| function ( $matches ) { | |
| global $post; | |
| $thumb = get_the_post_thumbnail( $post->ID, array( $matches[ 1 ], $matches[ 2 ] ) ); | |
| if ( ! $thumb ) { | |
| $default_thumb = get_option( "my_default_thumb" ); | |
| if ( $default_thumb ) { | |
| $thumb = "<img src=\"$default_thumb\" width=\"" . $matches[ 1 ] . "\" height=\"" . $matches[ 2 ] . "\">"; | |
| } | |
| } | |
| return $thumb; | |
| }, | |
| $text | |
| ); | |
| return $text; | |
| } | |
| } | |
| // parse any text of your choice | |
| $parsed_text = content_parser::parse( $some_custom_text ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class content_parser { | |
| /** | |
| * Parses text. | |
| * | |
| * @uses apply_filters() Calls 'content_parser_after' hook on the parsed text | |
| * | |
| * @param string $text Text to parse | |
| * @return string Parsed text | |
| */ | |
| public static function parse( $text ) { | |
| // ... your parsing code | |
| return apply_filters( 'content_parser_after', $text ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $text = str_replace( '%DATE%', get_the_date(), $text ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // custom post thumbnails | |
| $text = preg_replace_callback( | |
| '/%POST_THUMBNAIL\|(\d+)x(\d+)%/', | |
| function ( $matches ) { | |
| global $post; | |
| return get_the_post_thumbnail( $post->ID, array( $matches[ 1 ], $matches[ 2 ] ) ); | |
| }, | |
| $text | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment