最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

posts - wp_insert_post not working fine

programmeradmin3浏览0评论

I asked my question on stackoverflow but I did not get any answer, may be because it's the question related to Wordpress.So I am asking it here.

I am using wp_insert_post function to insert my posts, but it is not working fine for me.

Here is my post_content

$content = '<div class="snippet">
   <strong>Wordpress Tutorial - Jetpack Plugin installieren &amp; konfigurieren</strong>
   <br/>
   <p>Wordpress Online Kurs unter  Sie erhalten Tutorials zu den Wordpress Themen, wie Plugins, Theme, SEO ...<a href=;sa=t&url=;ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGFOBEtFiT4f7zq6xl-xTbsW9uIVg target="_blank"></a>
   <br />
   <a href=;sa=t&url=;ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGFOBEtFiT4f7zq6xl-xTbsW9uIVg rel="nofollow" target='_blank'>See Original Article </a>
   </p>
</div>
<div class="snippet">
   <strong>Preview Fargo - Responsive Creative WordPress Theme TFx</strong>
   <br/>
   <p>
      Preview and Download :  FARGO is a beautifully designed and visually ...<a href=;sa=t&url=;ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGlUblc7rKI0OjRLqe1CChEfbpYmQ target="_blank"></a>
   <br />
   <a href=;sa=t&url=;ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGlUblc7rKI0OjRLqe1CChEfbpYmQ rel="nofollow" target='_blank'>See Original Article </a>
   </p>
</div>';

When I am adding this as manually it is inserting while I am using cron to insert it just insert post up to "SEO"

Code is :

$my_post = array('post_title'=>'test post','post_content'=>$content);

Any suggestions to solve this?

I asked my question on stackoverflow but I did not get any answer, may be because it's the question related to Wordpress.So I am asking it here.

I am using wp_insert_post function to insert my posts, but it is not working fine for me.

Here is my post_content

$content = '<div class="snippet">
   <strong>Wordpress Tutorial - Jetpack Plugin installieren &amp; konfigurieren</strong>
   <br/>
   <p>Wordpress Online Kurs unter http://wordpress-online-kurs.de Sie erhalten Tutorials zu den Wordpress Themen, wie Plugins, Theme, SEO ...<a href=https://www.google/url?rct=j&sa=t&url=http://www.youtube/watch%3Fv%3DzjMtxin-hRA&ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGFOBEtFiT4f7zq6xl-xTbsW9uIVg target="_blank"></a>
   <br />
   <a href=https://www.google/url?rct=j&sa=t&url=http://www.youtube/watch%3Fv%3DzjMtxin-hRA&ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGFOBEtFiT4f7zq6xl-xTbsW9uIVg rel="nofollow" target='_blank'>See Original Article </a>
   </p>
</div>
<div class="snippet">
   <strong>Preview Fargo - Responsive Creative WordPress Theme TFx</strong>
   <br/>
   <p>
      Preview and Download : http://fxtheme/themes/fargo-responsive-creative-wordpress-theme-tfx FARGO is a beautifully designed and visually ...<a href=https://www.google/url?rct=j&sa=t&url=http://www.youtube/watch%3Fv%3DqjKCijxQR1M&ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGlUblc7rKI0OjRLqe1CChEfbpYmQ target="_blank"></a>
   <br />
   <a href=https://www.google/url?rct=j&sa=t&url=http://www.youtube/watch%3Fv%3DqjKCijxQR1M&ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGlUblc7rKI0OjRLqe1CChEfbpYmQ rel="nofollow" target='_blank'>See Original Article </a>
   </p>
</div>';

When I am adding this as manually it is inserting while I am using cron to insert it just insert post up to "SEO"

Code is :

$my_post = array('post_title'=>'test post','post_content'=>$content);

Any suggestions to solve this?

Share Improve this question edited Sep 22, 2014 at 6:29 Bhuvnesh Gupta asked Sep 22, 2014 at 5:20 Bhuvnesh GuptaBhuvnesh Gupta 3112 gold badges9 silver badges21 bronze badges 4
  • You have asked 19 questions here. Your question was most probably not answered due your code's formatting. Putting all your code in one line is totally unreadable, and everyone tends to skip this type of questions if they can't read or debug code. Please work on your code format before adding it to a post. Properly indent it, make use of line breaks, make the code as easy readable as possible for others to debug and help you. Someone like me should not be there to clean up your code for you. Thank you – Pieter Goosen Commented Sep 22, 2014 at 5:54
  • @PieterGoosen Ok i will take care of it. – Bhuvnesh Gupta Commented Sep 22, 2014 at 6:16
  • My initial thought is that the code may not be escaped properly. As we can't see how you genterate your $array array. Going down this thought process it would need to be $array = array('$content'); as a quick troubleshoot. Can you post all the code please? EDIT I also see that your href content is not in double quotes, I'm not sure if it would still work but according to my understanding it's best practice to do so. – Joshua Commented Sep 22, 2014 at 6:21
  • @Joshua $array is generated using some api to fetch RSS feed. It is the dummy data of one RSS. Also when i am inserting same data manually it is getting inserted so double quote is not a problem – Bhuvnesh Gupta Commented Sep 22, 2014 at 6:27
Add a comment  | 

2 Answers 2

Reset to default 1

As Joshua suggested in his comment, your code isn't escaped properly.

Just before "See Original Article" (in both snippets) you have target='_blank' on your links with single quotes. Replace that with double quotes target="_blank"

This is general idea of how wp_insert_post works

if(isset($_POST['submit']) && !empty($_POST['submit']))

    $title      = sanitize_text_field( $_POST['title'] );
    $content    = sanitize_text_field( $_POST['content'] );

    $args = array(
        'post_title'    => $title,
        'post_content'  => $content, // this is your  post content
        'post_status'   => 'publish', //publish, draft
        'post_type'     => 'post',  //write here your post type it may be post , or your  custom post type
    );

  $posts = wp_insert_post( $args );
发布评论

评论列表(0)

  1. 暂无评论