When posting html content below it posts fine with the admin, but any other user it strips all HTML tags
$my_post = array('post_title' => $title,
'post_content' => $content,
'post_category' => array(520),
'post_status' => 'Publish',
'post_name' => $url
);
$result = wp_insert_post( $my_post );
Is there a way to allow any user to post HTML tags.
I found that if I use this before the code, it works.
$sub_role = get_role( 'subscriber' );
$sub_role->add_cap( 'unfiltered_html', true );
Related
here's my code to submit post from frontend using ACF, all of this are working fine in desktop but when I tried to post using phone the image appears in media library but not in CPT, does anyone here have any idea? thanks
$user_info = get_userdata(get_current_user_id());
$args = array(
'post_id' => 'new_post',
'field_groups' => array(570), //it's the ID of your custom field Form
'form' => false,
'new_post' => array(
'post_type' => 'team_member',
'post_status' => 'pending',
'post_title' => $user_info->user_login),
);
acf_form( $args );
Function.php
function my_pre_save_post( $post_id ) {
// bail early if not a new post
if( $post_id !== 'new_post' ) {
return $post_id;
}
$my_post = array(
'post_type' => 'team_member'
);
// insert the post
$post_id = wp_insert_post( $my_post );
}
I have created a page successfully from my custom plugin and have successfully added a custom template to the template dropdown and associated successfully the template to that page.
The problem is that the template is not being loaded.
Here the code
$new_page = array(
'post_title' => __( 'Custom Page' ),
'post_name' => 'custom-page',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'menu_order' => 0,
// Assign page template
'page_template' => plugin_dir_path( __FILE__ ) . '/my-plugin-folder/my-plugin-custom-template.php'
);
// insert the post into the database
wp_insert_post( $new_page );
What am I doing wrong???
when page load will be searching for my-plugin-custom-template.php in the theme directory, so you will have to redirect that to your plugin directory by using the page_template filter like below the code.
function custom_redirect_page_template ($template) {
$post = get_post();
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
if ('my-plugin-custom-template.php' == basename ($page_template ))
$template = WP_PLUGIN_DIR . '/my-plugin-folder/my-plugin-custom-template.php';
return $template;
}
add_filter ('page_template', 'custom_redirect_page_template');
I am trying to add a button in wordpress that deletes a custom-post-type post with a specific title and current user as author.
Problem that occurring is that all the posts gets deleted, all the job_alert posts, not only for this specific author or with this title.
Can someone see why?
$delete_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$posts = new WP_Query( $delete_post );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
wp_delete_post( get_the_ID());
}
}
I also have this code that creates a post and that works great. Similar code.
$new_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$post_id = wp_insert_post( $new_post );
For deleting the particular post, here is the solution,
Please use name parameter instead of post_title in your query, then only it will return the required post which you want.
I have modified your code. Please find the updated code below:
$delete_post = array(
'post_type' => 'job_alert',
'name' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
Now, your required post will be returned.
Hope, this may be helpful to you and let me know, if you have any query.
Thanks.
i was developing some theme that could create new post on front end. my issue is that when the "add media" has uploaded the photo, everything works well, except that when i set the "set featured image" for that post is not showing on the "thumbnail" when the post is published.
this is the wp_editor code setup:
<?php
$setupev = array(
'media_buttons'=>1,
'textarea_name'=>'post_ne',
'tinymce' => true );
wp_editor('content here', 'create_nepost', $setupev);
?>
this is the code i use to publish new post:
$the_contnt = $_POST['post_ne'];
$new_post = array(
'post_title' => $get_title,
'post_content' => $the_contnt,
'post_date' => date('YmdHis'),
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,5)
);
wp_insert_post( $new_post );
is there some code to add-in to get the attachment thumbnail?
you need to use set_post_thumbnail() function. two things you require for this function are the $post_id which is the id you are providing to wp_insert_post() and $attach_id which is the id of attachment.
set_post_thumbnail( $post_id, $attach_id );
you already have $post_id, to find $attach_id
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'`
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
I have a created a wordpress plugin that needs to create a page dynamically.
When the plugin is activated a page is created COOKIE_POLICY.
From my reading I found that inserting into the DB is the best way. And below is the way to do it. However, when I activate the plugin, there is no page or post created.
I go this from:
http://codex.wordpress.org/Function_Reference/wp_insert_post
function create_policy() {
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['COOKIE_POLICY'] ),
'post_content' => $_POST['Here is all our cookie policy info'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,30 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );
This code is nearly correct. Below is the fixed code
function create_policy() {
$my_post = array(
'post_title' => 'cookiepolicy',
'post_content' => 'this is my content',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 3,4 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );