plugin development – How to store the third party script with HTML code in the wordpress custom input field?

Question

I have created a custom field on my custom post. Below is the code I am using for the custom field.

<?php 

function wpt_add_thirdpartyvideo_metaboxes() {
  add_meta_box(
    'wpt_thirdpartyvideo_extra',
    'Information',
    'wpt_thirdpartyvideo_extra',
    'partyvideo',
    'normal',
    'default'
  );  
}
add_action( 'add_meta_boxes', 'wpt_add_thirdpartyvideo_metaboxes' );

function wpt_thirdpartyvideo_extra() {
  global $post;
  // Nonce field to validate form request came from current site
  wp_nonce_field( basename( __FILE__ ), 'thirdpartyvideo_extra' );
  // Output the field
  echo '<table width="100%" border="0" cellspacing="0" cellpadding="10">
  <tr>
        <td>Video Script here </td>
        <td>
      <div class="form-check">
       <textarea name="thirdpartyvideoscript">' . esc_textarea( get_post_meta( $post->ID, 'thirdpartyvideoscript', true ) )  . '</textarea>
       
      </div>
      </td>
        </tr>   
    </table>';
}
function wpt_save_thirdpartyvideo_meta( $post_id, $post ) {
  if ( ! current_user_can( 'edit_post', $post_id ) ) {
    return $post_id;
  }
  if ( ! isset( $_POST['thirdpartyvideoscript'] ) ) {
    return $post_id;
  }

  $events_meta['thirdpartyvideoscript'] = esc_textarea( $_POST['thirdpartyvideoscript'] );
  
  foreach ( $events_meta as $key => $value ) :
    if ( 'revision' === $post->post_type ) {
      return;
    }
    if ( get_post_meta( $post_id, $key, false ) ) {
      update_post_meta( $post_id, $key, $value );
    } else {
      add_post_meta( $post_id, $key, $value);
    }
    if ( ! $value ) {
      delete_post_meta( $post_id, $key );
    }
  endforeach;
}
add_action( 'save_post', 'wpt_save_thirdpartyvideo_meta', 1, 2 );

 ?>

I have tried to add some dummy content and it’s working. Now I have to add the third-party video code which is script and HTML tags like below Third-party example.

<script src="//fast.wistia.com/embed/medias/s3lqfi0zn7.jsonp" async></script>
<script src="//fast.wistia.com/assets/external/E-v1.js" async></script>
<div class="wistia_embed wistia_async_s3lqfi0zn7" style="height:349px;width:620px">&nbsp;</div>

If I add the above code then I am getting the below output

&lt;script src=&quot;//fast.wistia.com/embed/medias/s3lqfi0zn7.jsonp&quot; async&gt;&lt;/script&gt;
    &lt;script src=&quot;//fast.wistia.com/assets/external/E-v1.js&quot; async&gt;&lt;/script&gt;
    &lt;div class=&quot;wistia_embed wistia_async_s3lqfi0zn7&quot; style=&quot;height:349px;width:620px&quot;&gt;&amp;nbsp;&lt;/div&gt;

0
Naren Verma 1 year 2022-02-21T00:49:16-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse