【问题标题】:Getting data from WordPress widget to modify post content从 WordPress 小部件获取数据以修改帖子内容
【发布时间】:2019-11-24 06:46:35
【问题描述】:

我正在尝试创建一个小部件,该小部件接受段落编号并在帖子的指定段落之后插入一些 HTML(用于广告)。例如,如果用户在第一个字段中输入 1,在第二个字段中输入 3,我希望我定义的一些标记出现在这些段落之后。

当我通过

手动创建数组时,我已经创建了小部件并设法使其工作
$paragraph_targets = array('1', '3');

insert_post_ads 方法中。

我坚持的一点是如何从小部件中获取段落编号到 insert_post_ads 方法中。

完整代码如下。

class Ad_Inserter extends WP_Widget
{

    public function __construct()
    {
        parent::__construct(
            'ad_inserter',
            'Ad Inserter',
            [
                'description' => 'Inserts ads in post content',
            ]
        );
        add_filter('the_content', [
            $this,
            'insert_post_ads',
        ]);
    }

    public function form($instance)
    {
        $instance = array_merge([
            'ad_1' => '',
            'ad_2' => '',
        ], $instance);
        ?>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('ad_1')); ?>">Ad 1 Position</label>
            <input type="text" name="<?php echo esc_attr($this->get_field_name('ad_1')); ?>" id="<?php echo esc_attr($this->get_field_name('ad_1')); ?>"
                   value="<?php echo esc_attr($instance['ad_1']); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('ad_2')); ?>">Ad 2 Position</label>
            <input type="text" name="<?php echo esc_attr($this->get_field_name('ad_2')); ?>" id="<?php echo esc_attr($this->get_field_name('ad_2')); ?>"
                   value="<?php echo esc_attr($instance['ad_2']); ?>">
        </p>

        <?php
    }

    public function widget($args, $instance)
    {

    }

    public function insert_post_ads($content)
    {
        if (is_single() && !is_admin()) {
            $paragraph_targets = [
                '1',
                '3',
            ];
            sort($paragraph_targets);

            return $this->insert_after_paragraph($paragraph_targets, $content);
        }

        return $content;
    }

    // Go through paragraphs and insert ads
    public function insert_after_paragraph($paragraph_targets, $content)
    {
        $ad_ids = [
            'advert-1',
            'advert-2',
        ];

        $closing_p = '</p>';
        $paragraphs = explode($closing_p, $content);

        foreach ($paragraphs as $index => $paragraph) {
            if (trim($paragraph)) {
                $paragraphs[$index] .= $closing_p;
            }

            if ($paragraph_targets[0] == $index + 1) {
                $ad_code = '<div id="'.$ad_ids[0].'" class="advert">**** AD ****</div>';
                $paragraphs[$index] .= $ad_code;
                array_shift($paragraph_targets);
                array_shift($ad_ids);
            }
        }

        return implode('', $paragraphs);
    }

}

add_action('widgets_init', 'registerWidget');
function registerWidget()
{
    register_widget('Ad_Inserter');
}

【问题讨论】:

    标签: php wordpress widget


    【解决方案1】:

    如果没有更好的答案,我可能会使用一个全局变量来存储它。假设 widgets_init 在创建帖子之前被触发。

    https://www.geeksforgeeks.org/how-to-declare-a-global-variable-in-php/

    更新

    当您的过滤器被调用/应用时,您只会收到 $content 对象,并且由于 $paragraph_targets 不是其中的一部分,除了将其设为全局变量之外,我不知道如何获得它。

    为了确定,你能解释一下应该发生什么的流程吗?用户输入段落编号,然后页面重新加载并添加更改?它不应该被存储吗?如果是这样,那么 $paragraph_targets 不应该存储为帖子元数据吗?

    那么你可能会像这样在insert_post_ads($content) 中得到它:

    $post_id = get_the_ID();
    get_post_meta( $post_id,'paragraph_targets');
    

    (未测试)

    【讨论】:

    • 为了进一步解释,用户将通过小部件页面激活小部件。这样小部件就会为所有帖子激活。一旦用户输入段落编号,任何从那里加载的帖子都会在指定的段落编号之后自动添加广告。
    • 如果我想自定义每个帖子的定位,我认为帖子元可能更适合,而在我的情况下,我希望为所有帖子设置一个设置。我设法找到了我想要的东西:get_option($this-&gt;option_name)。这将返回小部件值以及我需要删除的其他数据。虽然你的建议不是我想要的,但它帮助我找到了我想要的东西,所以感谢你的帮助!
    【解决方案2】:

    解决方案是get_option($this-&gt;option_name),它返回一个多维数组,其中包含我从小部件中获取的段落目标值。

    我必须创建一个新方法来从多维数组中提取值并将它们放入一个基本数组中。

    下面的解决方案。

            public function insert_post_ads($content) {
                if ( is_single() && ! is_admin() ) {
                    // $paragraph_targets = array('2', '5');
                    $paragraph_widget_values = get_option($this->option_name);
                    $paragraph_targets = $this->get_array_values($paragraph_widget_values);
    
                    array_pop($paragraph_targets); // Remove _multiwidget from array
                    sort($paragraph_targets);
                    return $this->insert_after_paragraph( $paragraph_targets, $content );
                }
                return $content;
            }
    
            // Get values from multi-dimensional array
            public function get_array_values($array) { 
                if (!is_array($array)) { 
                    return FALSE; 
                } 
                $result = array(); 
                foreach ($array as $key => $value) { 
                    if (is_array($value)) { 
                        $result = array_merge($result, $this->get_array_values($value)); 
                    } 
                    else { 
                        $result[$key] = $value; 
                    } 
                }
    
                return array_values($result); 
            } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      相关资源
      最近更新 更多