Skip to content

Instantly share code, notes, and snippets.

@dionysia
Created March 9, 2013 21:50
Show Gist options
  • Save dionysia/5125904 to your computer and use it in GitHub Desktop.
Save dionysia/5125904 to your computer and use it in GitHub Desktop.
From WP StackExchange: http://wordpress.stackexchange.com/questions/90066/numbering-sections-and-block-level-elements-in-wpautop-wordpress-as-cms-for-l I am starting to learn how to write plugins for Wordpress. I'm interested in using WP as a CMS for long essays (6000-9000 words or more). My problem is what I call the Kindle problem, which is ho…
class InsertAnchors {
protected $count = 0;
protected $links = '';
public function build( $pattern, $content ) {
$this->count = 0;
$this->links = '';
$content = preg_replace_callback( $pattern, array( $this, '_replacer' ), $content );
return '<p>' . $this->links . '</p>' . $content;
}
public function _replacer( $matches ) {
$this->count++;
$this->links .= '<a href="#section_' . $this->count . '">Section ' . $this->count . '</a><br>';
return '<p><a name="section_' . $this->count . '"></a>';
}
}
function anchors_content_filter( $content ){
$insert = new InsertAnchors();
return $insert->build( '~<p>~', $content );
}
add_filter( 'the_content', 'anchors_content_filter', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment