Create a wordpress post with PHP - 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.

Related

Create a page with gutenberg HTML block automatically upon plugin activation

I have managed to create a page upon plugin activation.
i need to add content to the page that get created. so i used following code. The problem is it creates a classic editor in the page and not the gutenberg HTML editor.
register_activation_hook( __FILE__, 'my_plugin_install_function');
function my_plugin_install_function()
{
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => get_current_user_id(),
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'Checklists',
'post_status' => 'publish' ,
'post_content' => '[customization-shortcode]',
'post_title' => 'Checklists',
'post_type' => 'dash',
);
//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
update_option( 'hclpage', $newvalue );
}
i expect to have my shortcode displayed in a gutenberg HTML block and not the classic editor
I have found a solution to this, all i did is change following line:
'post_content' => '[customization-shortcode]',
to
'post_content' => '<!-- wp:html -->[customization-shortcode]<!-- /wp:html -->',
This will create a usual gutenberg HTML block instead the old classic one.

Inserting Wordpress Post via External Script

I'm trying to insert bulk posts via an external php script:
<?php
require_once '/full/path/to/wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
$title = "some post title";
$content = "some content";
$tags= "tag1,tag2,tag3";
$user_id = 1;
// Create post object
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'post',
'tags_input' => $tags,
);
$id = wp_insert_post($my_post,true);
?>
$id returns 0 and WP_Error is empty.
Post is inserted to DB with right title and content but without tags. It also fails to use wp_insert_terms() to insert tags or other custom taxonomies.
Did i miss a file to include or is there something i didn't set right to work functions properly?
you don't need to load taxonomy.php.. this functionality is handled by wp-load.php
also i would suggest that you put this on first position in your file:
define( 'WP_USE_THEMES', false );
than you can use wp_insert_post() to add the post.. but to add the tags or category and so on you need to use
wp_set_post_terms()
or
wp_set_object_terms()
with your recieved $id from wp_insert_post.
for more informations check this: https://wordpress.stackexchange.com/questions/18236/attaching-taxonomy-data-to-post-with-wp-insert-post
Please include the wordpress database files first.
require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
and use the following code to insert post dynamically from the external script.
$newIds = wp_insert_post( array(
'post_title' => $postCSVContent['1'],
'post_content' => $postCSVContent['2'],
'post_type' => 'doors',
'post_status' => 'publish',
'post_author' => 1,
'post_parent' => $parentId
));
Refer the tutorial that explains how to create and update wordpress post programmatically.
http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

wp_post_insert HTML in post_content

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

WordPress wp_insert_post with HTML in post_content is adding \r\n

I'm creating a script outside of WordPress to insert new posts and WordPress is sanitizing my post in a weird way.
I will also be modifying the posts outside of WordPress.
I'm am using TinyMCE for the post_content textarea.
I'm using all WordPress functions by including:
include('../wp-config.php');
Here is my code that inserts my posts, but with \r\n for each new line:
$listing_phase = strtoupper($_POST['listing_phase']);
$listing_title = strtoupper($_POST['listing_title']);
$listing_description = $_POST['listing_description'];
$new_listing = array(
'post_title' => $listing_title,
'post_name' => str_replace('-', ' ', $listing_title),
'post_content' => $listing_description,
'tax_input' => array('property-status' => $listing_phase),
'post_status' => 'publish',
'post_type' => 'property',
'post_author' => 1
);
$listing_id = wp_insert_post($new_listing);
Here is the HTML that TinyMCE submitted:
<p>Studio</p>
<ul>
<li>Free Electric</li>
</ul>
This is what is in the database:
<p>Studio</p>\r\n<ul>\r\n<li>Free Electric</li>\r\n</ul>
When trying to modify a post I'm calling the_content(); from get_posts(); and \r\n are showing up.
How do I submit basic HTML content to WordPress's wp_insert_post function and get the content to view or modify later?
Thanks!
$listing_description = str_replace('\r\n','',$_POST['listing_description']);

how to assign page template dynamically in wordpress

I have a WordPress page created dynamically
$my_post = array(
'post_title' => 'page-for-download',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
now my question is how to assign page template to this page dynamically
WordPress keeps the page template in a post meta entry(named _wp_page_template). Here is what you should do once you create the page:
update_post_meta( $new_post_id, '_wp_page_template', 'custom-template.php' );
Where $new_post_id is the result of wp_insert_post()(I assume that this is what you are using to create the new post). Note, that you might want to check to see if you have an actual id(by default wp_insert_post() will return false if it fails to create a new post).
You can see that information in the first NOTE in the Parameters section of the WordPress codex page Function Reference/wp insert post

Categories