duanhui7329 2017-09-04 13:38
浏览 57
已采纳

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

I'm finding out if there is an plugin that can search a string in a specific file name (ex:/template-parts/content-home.php) and then replace the string to a new value without editing the code itself.

The reason for it is because I coded a Wordpress theme for a company. They want to edit the Vimeo URL on the front-end so they dont have to touch any code.

For example:

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

We want to replace the vimeo url, so it should be:

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

Here you can check the website: You see a Vimeo player on background.

http://happyinflorida.nl/

I created a screen impression so you can see what I mean with custom string replacement in Wordpress backend system. Hope you guys can put me in the right direction.

Screen impression Wordpress string replacement

If you need more information please feel free to ask.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongxiangchan0743 2017-09-04 14:32
    关注

    Ideally you would want to avoid hard-coding your video url into the template file. The "WordPress way" of doing this would be to add a custom meta box to your page(s) and add the video URL to that metabox and have it update your template file dynamically. Here is an example of how you might do this.

    This code can go into your child-theme's functions.php file:

    /**
     * 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


    Once this code is added you will notice a new meta-box in the backend of your page:

    enter image description here


    Now in your template file template-parts/content-home.php you can modify the code as follows:
    <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>
    

    Which will allow you to simply change the URL in the backend whenever it needs to be changed.

    You can change the meta-box values and titles to whatever suites your need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测