54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
/*-----------------------------------------------------------------------------------
|
||
|
|
|
||
|
|
Plugin Name: MyThemeShop After First Paragraph Ad Widget
|
||
|
|
Description: A widget area that displays after the first paragraph in posts/pages
|
||
|
|
Version: 1.0
|
||
|
|
|
||
|
|
-----------------------------------------------------------------------------------*/
|
||
|
|
|
||
|
|
// Register widget area
|
||
|
|
function mts_register_after_first_paragraph_widget() {
|
||
|
|
register_sidebar(array(
|
||
|
|
"name" => __("After First Paragraph", "mythemeshop"),
|
||
|
|
"id" => "after-first-paragraph",
|
||
|
|
"description" => __("Widget area that appears after the first paragraph in posts and pages.", "mythemeshop"),
|
||
|
|
"before_widget" => "<div class=\"after-first-paragraph-widget\">",
|
||
|
|
"after_widget" => "</div>",
|
||
|
|
"before_title" => "<h3 class=\"widget-title\">",
|
||
|
|
"after_title" => "</h3>",
|
||
|
|
));
|
||
|
|
}
|
||
|
|
add_action("widgets_init", "mts_register_after_first_paragraph_widget");
|
||
|
|
|
||
|
|
// Insert widget content after first paragraph
|
||
|
|
function mts_insert_after_first_paragraph($content) {
|
||
|
|
// Only run on single posts and pages
|
||
|
|
if (!is_single() && !is_page()) {
|
||
|
|
return $content;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check if widget area has active widgets
|
||
|
|
if (!is_active_sidebar("after-first-paragraph")) {
|
||
|
|
return $content;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get the widget content
|
||
|
|
ob_start();
|
||
|
|
dynamic_sidebar("after-first-paragraph");
|
||
|
|
$widget_content = ob_get_clean();
|
||
|
|
|
||
|
|
// Find the first paragraph
|
||
|
|
$closing_p = "</p>";
|
||
|
|
$paragraphs = explode($closing_p, $content);
|
||
|
|
|
||
|
|
// Insert widget after first paragraph if content has paragraphs
|
||
|
|
if (count($paragraphs) > 1) {
|
||
|
|
$paragraphs[0] .= $closing_p . $widget_content;
|
||
|
|
$content = implode($closing_p, $paragraphs);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $content;
|
||
|
|
}
|
||
|
|
add_filter("the_content", "mts_insert_after_first_paragraph");
|