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 http://wordpress-online-kurs.de Sie erhalten Tutorials zu den Wordpress Themen, wie Plugins, Theme, SEO ...<a href=https://www.google.com/url?rct=j&sa=t&url=http://www.youtube.com/watch%3Fv%3DzjMtxin-hRA&ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGFOBEtFiT4f7zq6xl-xTbsW9uIVg target="_blank"></a>
   <br />
   <a href=https://www.google.com/url?rct=j&sa=t&url=http://www.youtube.com/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.com/themes/fargo-responsive-creative-wordpress-theme-tfx FARGO is a beautifully designed and visually ...<a href=https://www.google.com/url?rct=j&sa=t&url=http://www.youtube.com/watch%3Fv%3DqjKCijxQR1M&ct=ga&cd=CAIyGmExYThkZTljMmU3MDQyZGE6Y29tOmVuOlVT&usg=AFQjCNGlUblc7rKI0OjRLqe1CChEfbpYmQ target="_blank"></a>
   <br />
   <a href=https://www.google.com/url?rct=j&sa=t&url=http://www.youtube.com/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?

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 Sep 22, 2014 at 5:54
  • @PieterGoosen Ok i will take care of it. 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
    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 Sep 22, 2014 at 6:27

2 Answers 2

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"

3
  • I tried but not worked, data is not inserting after SEO means ... is not working Sep 22, 2014 at 8:06
  • Can you please clarify what you mean by "after SEO"?
    – jzatt
    Sep 22, 2014 at 8:08
  • In the $content there is a term "SEO ..." when I am running wp_insert_post then post is created with content upto SEO not SEO ... means dots (.) are missing Sep 22, 2014 at 8:15
0

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.