在保存和考虑各种可能的标记的过程中,向内容中添加内容并不是一件容易的事。我对它进行了一些研究,并决定最好使用PHP native\\DOMDocument
类来解析内容、识别段落并向其添加HTML注释。这比使用正则表达式可靠得多,性能也更好。
首先调整插件,插件使用依赖注入来解耦类。如果需要更改输出(更改段落数、插入其他注释,如换行符或来自短代码的内容等),则需要跳入并调整Parser
从内部初始化的Controller
.
如果您想插入一些普通的HTML(例如,在X段落后插入广告),那么您需要进入Parser
并删除以下行:
$comment = $this->dom->appendChild( new \\DOMComment( $this->tag ) );
然后更换
$comment
在下一行中
$this->tag
. 然后你可以加入普通的HTML标签、文本或其他东西。
对于更复杂的内容,您需要利用DOMDocument
和类似的对象方法。请参考php。net获取更多信息。
注意:以下插件仅适用于PHP 5.3+。如果您使用的是早期版本,它将不会激活,而是显示WP die屏幕。
插件
<?php
namespace WPSE\\NextpageParagraph107787;
defined( \'ABSPATH\' ) OR exit;
/**
* Plugin Name: (#107787) Nextpage after X paragraphs
* Plugin URl: http://wordpress.stackexchange.com/questions/107787
* Description: <strong>Needs PHP 5.3+!</strong> Adds a <code><!--nextpage--></code> tag after X paragraphs.
* Author: Franz Josef Kaiser
* Author URl: http://unserkaiser.com
* License: MIT
*/
\\add_action( \'init\', array( __NAMESPACE__.\'\\Controller\', \'init\' ) );
class Controller
{
protected static $instance = null;
public static function init()
{
null === self::$instance AND self::$instance = new self;
return self::$instance;
}
protected function __construct()
{
$parser = new Parser();
$parser->setTag( \'nextpage\' );
$parser->setTagAmount( 5 );
\\add_action( \'load-post.php\', array( $parser, \'onSave\' ) );
\\add_action( \'load-post-new.php\', array( $parser, \'onSave\' ) );
}
}
class Parser
{
private $dom = null;
private $tag = null;
private $amount = null;
public function __construct( $tag = null, $paragraph_number = null )
{
null === $this->dom
AND $this->dom = new \\DOMDocument();
}
public function setTag( $tag )
{
$this->tag = $tag;
}
public function setTagAmount( $amount )
{
$this->amount = $amount;
}
public function onSave( $post_id )
{
if ( empty( $_POST[\'content\'] ) )
return;
$this->dom->loadHTML( \\wpautop( $_POST[\'content\'] ) );
$paragraph = $this->dom->getElementsByTagName( \'p\' );
$content = null;
$i = 1;
foreach ( $paragraph as $p )
{
$content .= $this->dom->saveHTML( $p );
if (
$this->amount === $i++
AND $this->amount < $paragraph->length
)
{
$comment = $this->dom->appendChild( new \\DOMComment( $this->tag ) );
$content .= $this->dom->saveHTML( $comment );
}
}
// Add to the HTTP $_POST global
$_POST[\'content\'] = $content;
}
}
\\register_activation_hook( __FILE__, array( __NAMESPACE__.\'\\Setup\', \'onActivation\' ) );
\\register_deactivation_hook( __FILE__, array( __NAMESPACE__.\'\\Setup\', \'onDeactivation\' ) );
\\register_activation_hook( __FILE__, array( __NAMESPACE__.\'\\Setup\', \'onUninstall\' ) );
class Setup
{
public function onActivation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "activate-plugin_{$plugin}" );
// do stuff
}
public function onDeactivation()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
$plugin = isset( $_REQUEST[\'plugin\'] ) ? $_REQUEST[\'plugin\'] : \'\';
check_admin_referer( "deactivate-plugin_{$plugin}" );
// do stuff
}
public function onUninstall()
{
if ( ! current_user_can( \'activate_plugins\' ) )
return;
check_admin_referer( \'bulk-plugins\' );
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
// do stuff
}
}