2017-09-04 29 views
1

我在找出是否有一個插件可以搜索特定文件名中的字符串(例如:/template-parts/content-home.php),然後將該字符串替換爲新值而不用編輯代碼本身。可能用插件替換子主題中的字符串?

這是因爲我編寫了一個公司的Wordpress主題。他們想要在前端編輯Vimeo網址,以便他們不必觸摸任何代碼。

例如:

<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

如果您需要了解更多信息,請隨時提問。

謝謝!

+0

也許只是不硬編碼嗎?在FrontPage的編輯器中添加一個字段,並從該輸入中獲取值/ url。在你的主題中迴應這個問題? – Stender

+0

感謝您的輸入Stender,似乎可行,其他投入也歡迎。 @Stender Wil看看這是否可能。保持更新 –

+0

@Stender請檢查在Und3rTow(它解決了我的問題)的答案 –

回答

0

理想情況下,您希望避免將視頻網址硬編碼到模板文件中。這樣做的「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


一旦代碼被加入,你會在你的頁面的後臺發現一個新的元框:

enter image description here


現在在您的模板文件 template-parts/content-home.php中,您可以將代碼修改爲fol低點:

<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。

您可以將元框值和標題更改爲您需要的任何套件。

+0

感謝您的評論,現在將閱讀此! @ Und3rTow –

+1

感謝您的回答!這是真正有幫助的隊友!清楚說明如何以正確的方式做到這一點。而且是一種很好且清晰的編碼方式。這解決了我的問題! –