【问题标题】:Possible to replace a string in child theme with a plugin?可以用插件替换子主题中的字符串吗?
【发布时间】:2018-02-12 18:24:05
【问题描述】:

我正在寻找是否有一个插件可以搜索特定文件名中的字符串(例如:/template-parts/content-home.php),然后将字符串替换为新值而不编辑代码本身。

原因是我为一家公司编写了一个 Wordpress 主题。他们想在前端编辑 Vimeo URL,这样他们就不必接触任何代码。

例如:

<div class="vimeo-div">
    <iframe src="https://OLD_URL.vimeo.com/video/OLD" width="830" height="467" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>

我们要替换vimeo url,所以应该是:

<div class="vimeo-div">
    <iframe src="https://NEW_URL.vimeo.com/video/NEW" width="830" height="467" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>

您可以在此处查看网站:您会在后台看到一个 Vimeo 播放器。

http://happyinflorida.nl/

我创建了一个屏幕印象,以便您了解我在 Wordpress 后端系统中使用自定义字符串替换的意思。希望你们能把我引向正确的方向。

Screen impression Wordpress string replacement

如果您需要更多信息,请随时询问。

谢谢!

【问题讨论】:

  • 也许只是没有硬编码?在首页的编辑器上添加一个字段,并从该输入中获取值/url。在你的主题中呼应这一点?
  • 感谢 Stender 的输入,看起来可行,也欢迎其他输入。 @Stender Wil 看看这是否可能。让你更新
  • @Stender 请检查 Und3rTow 的答案(它解决了我的问题)

标签: php wordpress string url replace


【解决方案1】:

理想情况下,您应该避免将视频网址硬编码到模板文件中。这样做的“WordPress 方式”是将自定义元框添加到您的页面并将视频 URL 添加到该元框并让它动态更新您的模板文件。这是您如何执行此操作的示例。

此代码可以进入您的子主题的functions.php 文件:

/**
 * Calls the class on the post edit screen.
 */
 function call_someClass() {
    new someClass();
}

if ( is_admin() ) {
    add_action( 'load-post.php',     'call_someClass' );
    add_action( 'load-post-new.php', 'call_someClass' );
}

/**
 * The Class.
 */
class someClass {

    /**
     * Hook into the appropriate actions when the class is constructed.
     */
    public function __construct() {
        add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
        add_action( 'save_post', array( $this, 'save' ) );
    }

    /**
     * Adds the meta box container.
     */
    public function add_meta_box( $post_type ) {
        // Limit meta box to certain post types.
        $post_types = array( 'post', 'page' );

        if ( in_array( $post_type, $post_types ) ) {
            add_meta_box(
                'some_meta_box_name',
                __( 'Some Meta Box Headline', 'textdomain' ),
                array( $this, 'render_meta_box_content' ),
                $post_type,
                'advanced',
                'high'
            );
        }
    }

    /**
     * Save the meta when the post is saved.
     *
     * @param int $post_id The ID of the post being saved.
     */
    public function save( $post_id ) {

        /*
         * We need to verify this came from the our screen and with proper authorization,
         * because save_post can be triggered at other times.
         */

        // Check if our nonce is set.
        if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
            return $post_id;
        }

        $nonce = $_POST['myplugin_inner_custom_box_nonce'];

        // Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
            return $post_id;
        }

        /*
         * If this is an autosave, our form has not been submitted,
         * so we don't want to do anything.
         */
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        // Check the user's permissions.
        if ( 'page' == $_POST['post_type'] ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

        /* OK, it's safe for us to save the data now. */

        // Sanitize the user input.
        $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );

        // Update the meta field.
        update_post_meta( $post_id, '_my_meta_value_key', $mydata );
    }


    /**
     * Render Meta Box content.
     *
     * @param WP_Post $post The post object.
     */
    public function render_meta_box_content( $post ) {

        // Add an nonce field so we can check for it later.
        wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );

        // Use get_post_meta to retrieve an existing value from the database.
        $value = get_post_meta( $post->ID, '_my_meta_value_key', true );

        // Display the form, using the current value.
        ?>
        <label for="myplugin_new_field">
            <?php _e( 'Description for this field', 'textdomain' ); ?>
        </label>
        <input type="text" id="myplugin_new_field" name="myplugin_new_field" value="<?php echo esc_attr( $value ); ?>" size="25" />
        <?php
    }
}

source


添加此代码后,您会在页面后端注意到一个新的元框:


现在在您的模板文件template-parts/content-home.php 中,您可以修改代码如下:
<div class="vimeo-div">
    <iframe src="<?php echo get_post_meta( $post->ID, '_my_meta_value_key', true ) ?>" width="830" height="467" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>

这将允许您在需要更改时简单地更改后端中的 URL。

您可以将元框值和标题更改为您需要的任何套件。

【讨论】:

  • 感谢您的回答!这真是个乐于助人的伙伴!清楚地说明如何以正确的方式做到这一点。以及一种漂亮而清晰的编码方式。这解决了我的问题!
猜你喜欢
  • 2017-10-29
  • 1970-01-01
  • 2014-12-11
  • 2023-01-10
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
相关资源
最近更新 更多