可以用插件替换子主题中的字符串吗?

时间:2017-09-04 13:38:31

标签: php wordpress string url replace

我发现是否有一个插件可以搜索特定文件名中的字符串(例如:/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

如果您需要更多信息,请随时提出。

谢谢!

1 个答案:

答案 0 :(得分:0)

理想情况下,您可能希望避免将视频网址硬编码到模板文件中。 &#34; WordPress方式&#34;这样做的目的是为您的页面添加一个自定义元框,并将视频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

<小时/> 添加此代码后,您会在页面后端看到一个新的元框:

enter image description here

<小时/> 现在,在模板文件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。

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