wp_post_insert HTML in post_content - php

I am trying to use wp_post_insert function to create posts/pages from my HTML pages.
So far I've got:
$html = file_get_contents($dir . $data['url']);
$post = array(
'post_content' => $html,
'post_name' => sanitize_title_with_dashes($data['title']),
'post_title' => $data['title'],
'post_status' => 'publish',
'post_type' => $data['type'],
);
$post_id = wp_insert_post($post);
However this does not seems to be working as post_content is always empty. I've tried different sanitation functions, but none of them are working.
HTML code is read into $html variable, but I guess the problem comes because its multiple lines.
Can you give me a hint?

Ive found that the files that I am reading were encoded with UCS2 encoding - file_get_contents returns special characters which looks good in browser (browser has its own character encoding), but not good for WP.
The solution was to reencode HTML files into UTF-8 ones.

Trick the function into thinking that the input has already been sanitized with the filter attribute:
$post = array(
'post_content' => $html,
'post_name' => sanitize_title_with_dashes($data['title']),
'post_title' => $data['title'],
'post_status' => 'publish',
'post_type' => $data['type'],
'filter' => true
);

Related

Create a wordpress post with PHP

I have a problem, I can create a new post in my WordPress blog using PHP code:
$new_post = array(
'post_title' => $leadTitle,
'post_content' => $leadContent,
'post_status' => $postStatus,
'post_date' => $timeStamp,
'post_author' => $userID,
'post_type' => $postType,
'post_category' => array($categoryID),
'tags_input' => array($tags)
);
$post_id = wp_insert_post($new_post);
My problem is that whenever post_content has HTML code in it, it does not get included with the post.
How can I include iframe tags within my post_content?
Use the iframe shortcode instead.
[iframe src=”http://www.youtube.com/embed/4qsGTXLnmKs” width=”100%” height=”500″]
WordPress removes iframe html tags because of security reasons.
Iframe shortcode is the replacement of the iframe html tag and accepts the same params as iframe html tag does.

Why the value of a variable is not entering post_content on just one occasion?

I have a file of autoimport.php
$identif = (string)$numbz;
$myembed = (string)$feed_item->embed;
$feed_item->desc .= "{$identif}{$myembed}<br>{$feed_item->desc}";
$post = array(
'post_author' => '1',
'post_status' => $_POST['status'],
'post_type' => $cpt,
'post_title' => (string)$feed_item->title,
'post_content' => (string)$feed_item->desc,
);
$post_id = wp_insert_post( $post );
update_post_meta($post_id, 'sponsor', (string)$_POST['thelink']);
update_post_meta($post_id, 'contID', (string)$feed_item->id);
There are two variables that I add in post_content in the publish action, always right with manual posting, but when the plugin uses another file to do the import of the post, to trigger the post, this variable $myembed does not enter at all in post_content. Why does it happen?
I make the change in the two files, all right in the manual post.
Importmanual.php has a code almost the same as above, with difference that use post_args:
$post_args = array(
'post_author' => '1',
'post_status' => $status,
'post_type' => $tca,
'post_title' => (string)$ivd['title'],
'post_content' => (string)$ivd['desc'],
);
$post_id = wp_insert_post( $post_args );
And the names of the variables are other, but I copy and paste, there is no writing error. Include the idea is so correct that the code works and the $identif variable comes together perfectly, what could it be?
see them complete: manual post file and auto post file Within the documents search for: ‘hercules’ and you will see a short explanation before the code. https://pastebin.com/s9Xcibd6

wordpress - Insert posts programmatically while maintaining links

I am currently working on a migration script to insert articles from XML into Wordpress.
So far I parsed the XML and created arrays in PHP, I am looping through these arrays and insert them all one by one into Wordpress with the following code:
$post = array(
'post_title' => wp_strip_all_tags($article['title']),
'post_content' => $article['description'],
'post_status' => 'publish',
'post_author' => 1,
'ping_status' => 'closed',
'post_date' => $dateTime->format('Y-m-d H:i:s'),
'post_type' => $post_type
);
$result = wp_insert_post($post);
That all goes well, however here comes the issue: the XML's are an export from a website (unfortunately I do not know which CMS ) and in the content there can be links to files on the same site, for example:
<![CDATA[<p><strong>Shortcuts:</strong></p>
<p/>
<ul>
<li>(Booklet in Finnish)
</li>
<li>(Booklet in Swedish)
</li>
<li>(Booklet in Estonian)
</li>
<li>(Booklet in Russian)
</li>
</ul>]]>
Testsite.fi is my own site, so these are internal links.
Those links are referring to PDF's and this should be inserted into wordpress, but obviously the links will be different. I do have the PDF's that are being referred to ( for example: elakkeen_hakeminen_ulkomailta.pdf, and they are in same folder as this script is ) so all that is required is to upload this file in Wordpress programmatically or manually move it to the correct location, and then update the links so that it still works.
Any clue how to do this? I am guessing something with regular expressions, but can't really figure it out.
To change all internal links you can use this:
$content = preg_replace('%href="http://www\.testsite\.fi/(.*)"%', 'href="' get_bloginfo('wpurl') . '/$1"', $article['description'], -1);
$post = array(
'post_title' => wp_strip_all_tags($article['title']),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'ping_status' => 'closed',
'post_date' => $dateTime->format('Y-m-d H:i:s'),
'post_type' => $post_type
);
$result = wp_insert_post($post);
Since the pdfs in your example do not have a filetype they can't be identified programmatically. Otherwise it would be something along the lines of:
$upload_dir = wp_upload_dir();
$content = preg_replace('%href="http://www\.testsite\.fi/(.*)/(.*).pdf"%', 'href="' . $upload_dir['url'] . '/$2.pdf"', $article['description'], -1);
where $2 is the filename for the pdf.
Note:
The href part in the regex is not neccesary but assures that you are not changing urls that are not inside a href atrribute. Depending on the scenario you can leave that part out.

wp_insert_post does not insert string with apostrophes correctly

I'm using wp_insert_post (for several reasons) in a script I'm writing and it's causing any string that has an apostrophe in it to be truncated up to that character (including the character). Here's the code I've got to test it:
$question = "If you’re in need, this is who you call";
echo $question."<br />";
$post = array(
'post_title' => $question,
'post_status' => 'publish',
'post_type' => 'question'
);
echo $post['post_title'];
$the_post_id = wp_insert_post($post);
It's weird because when I echo out both the strings, the echo the correct thing on both ends so I've isolated it down to the wp_insert_post function that's causing the issue. When I view it in WordPress, the title is just 'If you' and then ends.
I found a similar problem here:
http://premium.wpmudev.org/forums/topic/q-wp_insert_post-truncates-post-content-on-quotes-is-there-a-proper-way-to-escape-these-things
But there doesn't seem to be any solution to it on that page. Any ideas on what's going on here and how to solve it? No idea why it's causing it. I'm looking into it to see if has something to do with something else in my code but it seems like it shouldn't because the array element seems formatting just fine when it's echoed out before the wp_insert_post() function.
Thanks for your help!
Replace apostrophes with html 5 symbolt like ´ and try to store this is working for me.
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );

how to control guid url in wordpress

My title is kadhal-rojave-roja-2 but the guid id is http://sample.com/kadhal-rojave-roja-2/. I need it without dashes, like this:
http://sample.com/kadhal rojave roja 2/
How to do it?
This is my post code:
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_excerpt' => $excerpt,
'post_title' => $post_title,
'post_status' => 'publish',
);
$post_id = wp_insert_post($new_post);
This isn't possible, a browser will encode the spaces as %20 instead even if you do find a way to do this. Google also recommends using underscores or hyphens instead of spaces and so it is much better for search engine optimisation.
Also a URL could be much longer if you use %20 as it is 3 characters rather than 1.

Categories