I have listed the vimeo videos from a channel using vimeo api using php
Now i want to upload all the videos as a post to wordpress custom postype how to do that?
each time a new video i published it should add the videos as a post to custom postype
To create a post in WordPress, you can use the below code.
Step1:
Look for the Vimeo webhooks option, which will let you know when a new Vimeo video is published. By doing so, you can get the details of the video and then use that information to create a post in WordPress.
Vimeo API link: https://developer.vimeo.com/help
Zapier Vimeo Integration Link: https://zapier.com/apps/vimeo/integrations/webhook
Step2:
/**
* A function used to programmatically create a post in WordPress. The slug, author ID, and title
* are defined within the context of the function.
*
* #returns -1 if the post was never created, -2 if a post with the same title exists, or the ID
* of the post if successful.
*/
function programmatically_create_post() {
// Initialize the page ID to -1. This indicates no action has been taken.
$post_id = -1;
// Setup the author, slug, and title for the post
$author_id = 1;
$slug = 'example-post';
$title = 'My Example Post';
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'post'
)
);
// Otherwise, we'll stop
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
} // end if
} // end programmatically_create_post
add_filter( 'after_setup_theme', 'programmatically_create_post' );
Related
I created a custom post type with CPT plugin. It's works fine and did the job.
I can create post, publish the post and i can see on the site.
But when i create a post from frontend via wp_insert_post it's creates the post but i can't see on site. It gives me 404.
$postId = "" . getUserCompanyId() . $menuId . "";
$m = array(
'insert_id' => intval($postId),
'post_title' => $menuName . ' | ' . $postId,
'post_name' => ''.$postId.'',
'post_content' => '',
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_type' => 'm',
);
$insert = wp_insert_post( $m );
When i create a post:
When i publish and see on site:
Note: wp_insert_post works just fine when change post_type to post
insert_id is not allowed in post arguments array($m in your code) it must be ID.
But as you mentioned you need to create a post then ID is not required it is only needed when you wish to update an existing post.
Refer here for wp_insert_post(...)
Also, validate whether the post is created on not
$post_args = array('post_type' => 'your_custom_post',
/*other default parameters you want to set*/
);
$post_id = wp_insert_post($post_args);
if(!is_wp_error($post_id)){
//the post is valid
} else {
//there was an error in the post insertion,
echo $post_id->get_error_message();
}
I'm trying to automatically great a custom post (users) upon registering a new user to my site. I'm just barely familar with php, but I've been working off another question from stackoverflow: Automatically create a post for each user using wp_insert_post
I would ideally love to create a page upon registering or updating user information that carries over custom fields. I've used ACF to create custom fields associated with users (listed in the code as lowercase variables) and custom fields associated with the to be created custom posts (listed in the code as uppercase variables).
Thank you for any help you can offer!
function create_authors_page( $user_id ) {
$the_user = get_userdata( $user_id );
$new_user_name = $the_user->user_login;
$PostSlug = $user_id;
$PostGuid = home_url() . "/" . $PostSlug;
$member_bio = get_field('member_bio');
$contact_info = get_field('contact_info');
$member_affiliation = get_field('member_affiliation');
$my_post = array( 'post_title' => $new_user_name,
'post_type' => 'users',
'post_content' => '',
'post_status' => 'publish',
'post_theme' => 'user-profile',
'guid' => $PostGuid );
$NewPostID = wp_insert_post( $my_post );
$Member_Bio = $member_bio;
$Contact_Info = $contact_info;
$Member_Affiliation = $member_affiliation;
update_post_meta( $NewPostID, $Member_Bio, $Contact_Info, $Member_Affiliation );
return $NewPostID;
}
add_action('publish_members', 'create_authors_page');
Your hook is incorrect, use user_register action hook which fires after a user has registered and passes $user_id as a variable:
add_action('user_register', 'create_authors_page');
function create_authors_page( $user_id ) {
// do your stuff
}
You can also use profile_update hook that trigger each time user update profile.
I am trying to figure out how to create a Wordpress plugin that adds a page to the Wordpress website on which it is installed.
I came up with the following which works and adds the page.
However, it's far from what I am trying to achieve:
How to change the entire page? Not just the content? Right now, it has all the header and footer and navbar.
How to have PHP code in the page, not just static content?
Is it possible to have everything under a url (https://some-url.com/my-plugin/) routed to this same page?
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc.
<?php
/**
* Plugin Name: MyPlugin
* Plugin URI: myplugin.com
* Description: MyPlugin
* Version: 1.0
* Author: Mike
* Author URI: myplugin.com
*/
define( 'MYPLUGIN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function create_page() {
$post_data = array(
'post_title' => 'Test of my plugin',
# How to change the entire page? Not just the content?
# Also, how to have PHP code in the page, not just static content?
'post_content' => 'Place all your body content for the post in this line.',
'post_status' => 'publish', // Automatically publish the post.
'post_type' => 'page', // defaults to "post".
'post_name' => 'my-plugin', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugin-page?/
);
// Lets insert the page now.
wp_insert_post( $post_data );
}
function plugin_activation() {
create__page();
}
function plugin_deactivation() {
}
register_activation_hook( __FILE__, 'plugin_activation' );
register_deactivation_hook( __FILE__, 'plugin_deactivation' );
You want wp_insert_post()
Usage:
$postarr = [
'post_title' => 'My Page Title', // <title> tag
'post_content' => '<p>page content</p>', // html for page content
'post_type' => 'page', // blog post is default
'post_name' => 'my-plugion-page', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugion-page?/
];
$post_id = wp_insert_post($postarr);
For example, in your $postarr if you want it to be a page (not a blog post), use 'post_type' => 'page'. For the page title 'post_title' => 'My New Page' and so on.
See: https://developer.wordpress.org/reference/functions/wp_insert_post/
Updated for additions to user question
If you don't want it to follow the theme templates, then you'll need to hook into the 'page_template' filter and use a template file you create in your plugin. See: https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
Depends on the situation, you can add code in your custom template, use shortcodes or custom Gutenberg blocks. You can use Advanced Custom Fields on in the Admin UI and use get_field() in your code, etc. You have lots of options in WordPress. Once you have a custom template you can just add regular PHP in there.
Yes, but you'll need to use regex and create rewrite rules using add_rewrite_rule(). Here are links that help with creating rewrite rules in WordPress
add_rewrite_rule
add_rewrite_tag
flush_rewrite_rules
WP_Rewrite API
I'm building a WordPress Page for course registration. All I want the plugin to do is send the filled in form details to my email ID and send an email to the user that he/she has successfully registered for the course. I don't need users to signup with username and password.
I've tried my luck with WP Forms but it only seems to have the option to forward the email to me and not the user.
Any suggestion on which plugin I should use?
As #Hughes mentioned, you cant use wpcf7, and just hook on it to insert custom post on every query.
// Hook on wpcf7
add_filter( 'wpcf7_mail_components', 'do_on_cf7_submit', 50, 2 );
function do_on_cf7_submit($mail_params, $form = null) {
// Empty post content
$content = '';
// set post content if field not empty
if ($_POST['field-name'] != '') {
$content .= 'Field Name Label: '.$_POST['field-name'] ;
}
// insert post if content not epmty
if ($content != '') {
insertQueryPost($_POST['email'], $content);
}
// allow cf7 to do his stuff
return $mail_params;
}
// insert custom post type "query", don't forget to setup your custom post type first
function insertQueryPost($title, $content) {
// insted of proper post slug, just make a hashed slug, when setting custom post type, set it to not public and not search-able
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $content,
'post_type' => 'query',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
}
I have a gravity form utilizing an add-on upload field that post-submission does some background work to send a video file to YouTube, then return the YouTube video ID.
I am trying to set a transient value on gform submission that stores the post ID, so another hook related to this YouTube upload process can take the YouTube ID it gets, then update a custom field for the aforementioned post.
Setting the transient works and I can see it in wp_options, but when I try to get the transient, it continually comes back empty despite the key values being identical.
function action_prso_gform_youtube_uploader_pre_update_meta( $field_values, $this_data ) {
//vars
$gforms_entry_title = strtolower(preg_replace('/-+/', '-', preg_replace('/[^\wáéíóú]/', '-', $this_data['gforms_entry'][1])));
//Get new post id so we can update it's meta
$transient_post_id = NULL;
$key = 'adv_' . $gforms_entry_title;
$transient_post_id = get_transient($key);
//You have the post id, save the youtube id as post meta
$yt_video_id = $field_values[6][0]['video_id'];
update_post_meta( $transient_post_id, 'video_testimonial_youtube_data', $yt_video_id );
}
add_action( 'prso_gform_youtube_uploader_pre_update_meta', 'action_prso_gform_youtube_uploader_pre_update_meta', 10, 2 );
add_action("gform_after_submission_19", "submit_video_testimonial_from_submission", 5, 2);
function submit_video_testimonial_from_submission($entry, $form) {
// Create a new post under the "video-testimonial" post-type
$new_video_testimonal = array(
'post_title' => ucwords($entry[1]),
'post_status' => 'draft',
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'video-testimonial'
);
$gforms_entry_title = strtolower(preg_replace('/-+/', '-', preg_replace('/[^\wáéíóú]/', '-', $entry[1])));
$theId = wp_insert_post($new_video_testimonal);
// Now we add the meta to the custom fields
$thePrefix = 'video_testimonial_';
update_post_meta($theId, $thePrefix.'title', ucwords($entry[1]));
update_post_meta($theId, $thePrefix.'description', ucwords($entry[2]));
update_post_meta($theId, $thePrefix.'name', ucwords($entry[3]));
update_post_meta($theId, $thePrefix.'location', ucwords($entry[5]));
// Set a transient to pass post ID for YouTube api function
set_transient('adv_'.$gforms_entry_title, $theId, 500);
}
I've set error_log on every field, including $transient_post_id and; it records nothing.