uploading image from ACF image thru phone not working - wordpress

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

Related

Create a post after user registration in wordpress

I need to create a post after user registration with some roles in wordpress !
i used this code
add_action( 'user_register', 'create_resume', 10, 1 );
function create_resume( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added
$this_user_role = implode(', ', $user_roles );
if ($this_user_role == 'candidate') {
// Create a new post
$user_post = array(
'post_title' => $user_info->username,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'resume', // <- change to your cpt
'post_author' => '$Post_id',
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
but dosn't work ! :-(
anybody can help me ?
try this:
add_action( 'user_register', 'create_resume', 10, 1 );
function create_resume( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added
$has_candidate_role = in_array('candidate', $user_roles);
if ($has_candidate_role) {
// Create a new post
$user_post = array(
'post_title' => $user_info->username,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'resume', // <- change to your cpt
'post_author' => $user_id,
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
i can create the post with this code [after user registration]:
add_action('user_register', 'resume_create', 10, 1);
function resume_create($user_id)
{
// Get user info
$user_meta = get_userdata($user_name);
// Create a new post
$user_post = [
'post_title' => $_POST['slug'],
'post_type' => 'resume',
'post_status' => 'publish',
'post_author' => $post_ID,
];
// Insert the post into the database
$post_id = wp_insert_post($user_post);
}
But, not assign to curent user and special role.

How to add unfiltered_html capability

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

how to copy content from one post type to another?

I am trying to copy certain fields from a events custom post type to a standard blog post whenever an event is created or edited. I now have it working to the point that when an event is updated or edited it will create a blog post but if I am adding a brand new event it adds 2 blog post before adding the correct post with content. The first post it creates is auto draft then it creates one with the title and author but no content. The 3rd post it creates is correct though. How can I prevent it from creating these extra post when I add a new event?
This is my code so far:
function create_event_post( $post_id ) {
// If this is just a revision, don't create a new post.
if ( wp_is_post_revision( $post_id ) )
return;
// Set the title, thumbnail id, author, and content variables
$post_title = get_the_title( $post_id );
$post_type = get_post_type($post_id);
$post_content = get_post_field('post_content', $post_id);
$thumbnail_id = get_post_thumbnail_id( $post_id );
$author_id = get_post_field ('post_author', $post_id);
$author_name = get_the_author_meta( 'display_name' , $author_id );
// Set the variables for the new post to the same as the event post
$new_post = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_type' => 'post'
);
// If the post is not "tribe_events", don't create a new post.
if ( "tribe_events" != $post_type )
return;
// Create the new post and retrieve the id of the new post
$new_post_id = wp_insert_post ( $new_post );
// Set the featured image for the new post to the same image as event post
set_post_thumbnail( $new_post_id, $thumbnail_id );
}
add_action( 'save_post', 'create_event_post' );
Got it working. Here is the final code if anyone wants to see how it works.
add_action( 'save_post', 'create_event_post' );
function create_event_post( $post_id ) {
// Set the title, thumbnail id, author, and content variables
$post_title = get_the_title( $post_id );
$post_type = get_post_type($post_id);
$post_content = get_post_field('post_content', $post_id);
$thumbnail_id = get_post_thumbnail_id( $post_id );
$author_id = get_post_field ('post_author', $post_id);
// If the post is not "tribe_events", don't create a new post.
if ( "tribe_events" != $post_type )
return;
$new_post = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_type' => 'post'
);
remove_action( 'save_post', 'create_event_post' );
$post_exists = get_page_by_title( $post_title, $output, "post" );
if ( !empty($post_exists) ) {
// Update post
$update_post = array(
'ID' => $post_exists->ID,
'post_title' => $post_title,
'post_content' => $post_content,
);
// Update the post into the database
wp_update_post( $update_post );
set_post_thumbnail( $post_exists->ID, $thumbnail_id );
}
else {
// Create the new post and retrieve the id of the new post
$new_post_id = wp_insert_post ( $new_post );
// Set the featured image for the new post to the same image as event post
set_post_thumbnail( $new_post_id, $thumbnail_id );
}
// Now hook the action
add_action( 'save_post', 'create_event_post' );
}

Need example code for wp_playlist_shortcode

I trying to use wp_playlist_shortcode for creating playlist with audio-files from all blog posts.
In official documentation i saw this parameter:
'ids'
(array) Create a playlist out of these explicit attachment IDs. If empty, a playlist will be created from all $type attachments of $id. Default empty.
I trying this code, and its doesn't working:
$attch_id = array('76', '73', '70', '67');
wp_playlist_shortcode( array( 'ids' => '$attch_id' );
How to create playlist with audio-files from all blog posts? Now i use this code, but it is a not playlist.
$audios = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => null, 'post_mime_type' => 'audio/mpeg' );
$attachments = get_posts( $audios );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
$media_url = $post->guid;
$media_title = $post->post_title;
echo wp_audio_shortcode( array( 'src' => $media_url) );
echo '<p>' . $media_title . '</p>';
// print_r($media_url);
}
}
wp_reset_postdata();

Attach image to post in Wordpress XMLRPC

I am using XMLRPC to do posts to Wordpress. I am having issues posting thumbnails, after debugging wordpress code I see that my issue is caused by the fact that the image is not attached to the post.
I must do this without patching wordpress or using PHP, only iwth XMLRPC.
I can upload an image and get the ID of the image.
Other point that confuses me is how do you attach an image to a post that you did not posted yet because you wait for the image to upload? I am supposed to upload image then post ,then using the image id and the post id do an update on the image metadata?
Edit: the code in wordpress that is problematic is this check
if ( $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) )
and my assumption it is that it fails because the image is Unattached, if i fix that code all is fine but I can't patch the WP of my application users(so this is not a solution)
Yes it is possible to do it, if Wordpress version is 3.5 or greater,when using the code for uploading file/image you can set the post_id.
The flow I used for new posts with featured images is like this:
use the newPost function and post the content without the featured
image and also set publish to false, record the post_id returned by
this
upload the image and set the post_id to the id of the post just
posted, record the image_id
when done edit the post and set the wp_post_thumbnail equal to the
image_id you just uploaded and also set publish to true(if needed)
Important:
The mime type is important, it must be "image/jpg" or "image/png" please see documentation, if mime type is worng like "jpg" attaching will fail.
Tip:
For debugging, if you get a generic error from wordpress and you can't figure out why you can check the wordpress code and even edit it, adding debugging/tracing calls and hopefully you can figure out the cause.
This is an example of a post with category, image and tags. It requires class-IXR.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php
and mime_content_type function
https://github.com/caiofior/storebaby/blob/master/magmi/plugins/extra/general/socialnotify/wp/mimetype.php
$client = new IXR_Client($url);
$content = array(
'post_status' => 'draft',
'post_type' => 'post',
'post_title' => 'Title',
'post_content' => 'Message',
// categories ids
'terms' => array('category' => array(3))
);
$params = array(0, $username, $password, $content);
$client->query('wp.newPost', $params);
$post_id = $client->getResponse();
$content = array(
'name' => basename('/var/www/sb/img.jpeg'),
'type' => mime_content_type('/var/www/sb/img.jpeg'),
'bits' => new IXR_Base64(file_get_contents('/var/www/sb/img.jpeg')),
true
);
$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content);
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
// Tags
'mt_keywords' => 'tag1, tag2, tag3',
'wp_post_thumbnail' => $media['id']
);
$client->query('metaWeblog.editPost', $post_id, $username, $password, $content, true);
My version if you want to use only wp.newPost and wp.editPost
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
$usr = 'username_on_the_server_side';
$pwd = 'password_on_the_server_side';
$xmlrpc = 'server side xmlrpc.php url';
$client = new IXR_Client($xmlrpc);
//////////// IMAGE UPLOAD AND ATTACHMENT POST CREATION ///////////
$img_attach = 'link to the image';
$img_attach_content = array(
'name' => basename($img_attach),
'type' => mime_content_type($img_attach),
'bits' => new IXR_Base64(file_get_contents($img_attach)),
);
$status = $client->query( 'wp.uploadFile','1', $usr, $pwd, $img_attach_content );
$image_returnInfo = $client ->getResponse();
//////////// POST CREATION ///////////
$custom_fields = array(
array( 'key' => 'blabla1', 'value' => 'blabla1_value' ),
array( 'key' => 'blabla12', 'value' => 'blabla1_value2')
);
$post_content = array(
'post_type' => 'post',
'post_status' => 'draft', //for now
'post_title' => 'XMLRPC Test',
'post_author' => 3,
'post_name' => 'XMLRPC Test',
'post_content' => 'XMLRPC Test Content',
'custom_fields' => $custom_fields
);
$res = $client -> query('wp.newPost',1, $usr, $pwd, $post_content);
$postID = $client->getResponse();
if(!$res)
echo 'Something went wrong....';
else {
echo 'The Project Created Successfully('.$res.')<br>Post ID is '.$postID.'<br>';
}
//////////// Image Post Attachment Edit ///////////
$img_attach_content2 = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_title' => $postID,
'post_name' => $postID,
'post_parent' => $postID,
'guid' => $image_returnInfo['url'],
'post_content' => '',
'post_mime_type' => 'image/jpg'
);
$res2 = $client -> query('wp.editPost', 0, $usr, $pwd, $image_returnInfo['id'], $img_attach_content2);
$postIDimg = $client->getResponse();
//////////// POST EDIT ///////////
$post_content2 = array(
'post_status' => 'publish', //publish
'wp_post_thumbnail' => $image_returnInfo['id'],
'custom_fields' => array( 'key' => '_thumbnail_id', 'value' => $image_returnInfo['id'] )
);
$media2= $client->query('wp.editPost',0, $usr, $pwd, $postID, $post_content2);
This is my version, using wp.newPost and wp.editPost, added on WordPress 3.4, that allow the use of custom post types.
require_once("IXR_Library.php.inc");
$title = 'My title';
$body = 'My body';
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format
$title = htmlentities($title,ENT_NOQUOTES,#$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,#$encoding);
$content = array(
'post_title'=>$title,
'post_content'=>$body,
'post_type'=>'some_custom_post_type',
'post_status' => 'draft', // http://codex.wordpress.org/Post_Status
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);
// Create the client object
$client = new IXR_Client('http://example.com/xmlrpc.php');
$username = "wp_username";
$password = "wp_username_password";
$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'
if (!$client->query('wp.newPost', $params)) {
die('<br/><strong>Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
}
else
{
$post_id = $client->getResponse();
echo 'Inserted with id'.$post_id;
$picture = '/full/path/to/pic.jpg';
$content = array(
'name' => basename($picture),
'type' => mime_content_type($picture),
'bits' => new IXR_Base64(file_get_contents($picture)),
true
);
if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) {
die('<br/><strong>Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
}
else
{
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
'post_thumbnail' => $media['id']
);
if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) {
die('<br/><strong>Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
}
}
}

Resources