Для этих целей существует хук-фильтр контента. Ниже приведён полный код небольшого плагина, который создаёт свой виджет, и выводит его на определённых страницах после втрого абзаца.
Код можно скопировать в файл some-name.php
и поместить в папку /wp-content/plugins/
, затем активировать плагин. Вместо кастомного виджета можно использовать любой системный.
Результат работы можно посмотреть на странице http://test.kagg.eu/so572791/
.
<?php
/**
* Widget in the post
*
* Plugin Name: Widget in Post
* Plugin URI: https://wordpress.org/plugins/widget-in-post/
* Description: Shows widget as the second paragraph in the post.
* Author: KAGG Design
* Author URI: https://profiles.wordpress.org/kagg.eu/
* Requires at least: 5.0
* Tested up to: 5.4
* Version: 1.0.0
* Stable tag: 1.0.0
*
* Text Domain: widget-in-post
* Domain Path: /languages/
*
* @package widget-in-post
* @author KAGG Design
*/
/**
* Class WIP_Widget
*/
class WIP_Widget extends WP_Widget {
/**
* WIP_Widget constructor.
*/
public function __construct() {
parent::__construct(
'wip_widget',
'WIP Widget Title',
[ 'description' => 'WIP Widget description' ]
);
}
/**
* Frontend.
*
* @param array $args Arguments.
* @param array $instance Instance.
*/
public function widget( $args, $instance ) {
$title = isset( $instance['title'] ) ? $instance['title'] : __( 'Default Widget Title', 'wip-widget' );
$title = apply_filters( 'widget_title', $title );
echo wp_kses_post( $args['before_widget'] );
if ( ! empty( $title ) ) {
echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] );
}
echo wp_kses_post( '<strong>Some text to add via WIP Widget</strong>' );
echo wp_kses_post( $args['after_widget'] );
}
/**
* Backend.
*
* @param array $instance Instance.
*/
public function form( $instance ) {
$title = isset( $instance['title'] ) ? $instance['title'] : __( 'Default Widget Title', 'wip-widget' );
?>
<p>
<label for="<?php echo esc_html( $this->get_field_id( 'title' ) ); ?>">
<?php echo esc_html__( 'Title:', 'wip-widget' ); ?>
</label>
<input
class="widefat" id="<?php echo esc_html( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_html( $this->get_field_name( 'title' ) ); ?>" type="text"
value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Save widget settings.
*
* @param array $new_instance New settings.
* @param array $old_instance Old settings.
*
* @return array
* @see WP_Widget::update()
*/
public function update( $new_instance, $old_instance ) {
$instance = [];
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
return $instance;
}
}
/**
* The content filter.
*
* @param string $content Post content.
*
* @return string
*/
function wip_the_content_filter( $content ) {
global $post;
$insert_after = 2; // Paragraph number to insert widget after.
$allowed_pages = [ 'so572791', 'some-other-page-slug' ]; // Pages to work.
if ( ! isset( $post ) || ! in_array( $post->post_name, $allowed_pages, true ) ) {
return $content;
}
ob_start();
the_widget( 'WIP_Widget' );
$addon = ob_get_clean();
$content_arr = explode( '</p>', $content );
array_splice( $content_arr, $insert_after, 0, $addon );
return implode( '</p>', $content_arr );
}
add_filter( 'the_content', 'wip_the_content_filter' );
/**
* Register widget.
*/
function register_wip_widget() {
register_widget( 'WIP_Widget' );
}
add_action( 'widgets_init', 'register_wip_widget' );