ACF get_field() returns empty - php

I'm developing an application through custom post type with custom fields using ACF plugin. When the custom post type is saved, it will create a blog in my network which works great however, when trying to copy the custom fields from the custom post type to the newly created blog's homepage with same acf custom fields, the acf custom fields are not saved.
I tested, $station_description = 'Not Empty'; and it does save/copied to its specific field without a problem. Its just that when i use, get_field() it does not save/copied to the new created blog.
What's wrong with my code?
function myy_acf_save_post( $post_id ) {
$post = get_post($post_id);
if( $post->post_type == 'network_directory' ) {
$title = $post->post_title;
$post_slug = $post->post_name;
$client_username = get_field('client_username', $post_id);
$station_server = get_field('station_server', $post_id);
$station_location = get_field('station_location', $post_id);
// $station_description = get_field('station_description', $post_id);
$station_description = 'Not Empty';
$language = get_field('language', $post_id);
$station_email = get_field('station_email', $post_id);
$station_website = get_field('station_website', $post_id);
$station_port = get_field('station_port', $post_id);
$station_logo = get_field('station_logo', $post_id);
// $station_logo_url = wp_get_attachment_url( $station_logo );
// $subdomain = get_field('subdomain', $post->ID);
$main_site = 'domain.com';
$subdomain_install = true;
# Create a new user
$check_user_id = get_user_id_from_string( $station_email );
if ($check_user_id !== null) {
$user_id = get_user_id_from_string( $station_email );
} else {
$rand_number = rand( 1, 2000 );
$username = 'user-' . $rand_number;
$password = wp_generate_password( 12, false );
$email = $station_email;
$user_id = wpmu_create_user( $username, $password, $email );
}
// wp_new_user_notification( $user_id, $password );
# Create site
if( $subdomain_install )
{
$newdomain = "{$post_slug}.$main_site";
$path = '/';
}
$the_blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ) );
if ( ! add_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id, true ) ) {
update_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id );
}
switch_to_blog( $the_blog_id );
$homepage_id = get_option( 'page_on_front' );
// $station_logo_new_src = media_sideload_image($station_logo_url, $homepage_id, 'Station Logo','src');
// $station_logo_new_id = get_attachment_id_from_src($station_logo_new_src);
update_field('client_username', $client_username, $homepage_id);
update_field('station_server', $station_server, $homepage_id);
update_field('station_location', $station_location, $homepage_id);
update_field('language', $language, $homepage_id);
update_field('station_email', $station_email, $homepage_id);
update_field('station_website', $station_website, $homepage_id);
update_field('station_port', $station_port, $homepage_id);
// update_field('station_logo', $station_logo_new_id, $homepage_id);
update_field('station_description', $station_description, $homepage_id);
restore_current_blog();
}
}
add_action('acf/save_post', 'myy_acf_save_post', 9999);

throwing print_r(get_post_meta(get_the_ID())) in your template will output fields that are available, which can be useful debugging. In my case, after re-ordering modules on a page i noticed that get_post_meta output was unexpected, which then helped me see that i had forgotten to reset a query higher up the page using wp_reset_postdata().

fount it.
using get_post_meta to retrieve values is way better than get_field

Related

Change get_author_posts_url / Author URL via filter in Wordpress

I would like to modify the get_author_posts_url function to get a custom taxonomy guest author slug that I have created.
Is there a way to change the following function to fetch a custom taxonomy link? I have used a filter to register the guest author as the post author by using a custom taxonomy, but I couldn't find a way to connect it to this get_author_posts_url function:
function get_author_posts_url( $author_id, $author_nicename = '' ) {
global $wp_rewrite;
$author_id = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty( $link ) ) {
$file = home_url( '/' );
$link = $file . '?author=' . $author_id;
} else {
if ( '' === $author_nicename ) {
$user = get_userdata( $author_id );
if ( ! empty( $user->user_nicename ) ) {
$author_nicename = $user->user_nicename;
}
}
$link = str_replace( '%author%', $author_nicename, $link );
$link = home_url( user_trailingslashit( $link ) );
}
Besides using the following filter, I also added the custom taxonomy (autoria) as the base slug instead of the /author slug. It's all working in the way that it is registering the custom author as the post author and also creating a custom taxonomy link in which I can access all posts by a certain custom author, but whenever I try to fetch the Author_URL it still shows the user that created the post (admin).
The filter I'm using is:
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = wp_get_post_terms( $post->ID, 'autoria', array( 'fields' => 'names' ) );
$author = implode( ', ', $author );
if ( $author )
$name = $author;
return $name;
}
Is there a way to connect this guest_author_name function to register the URL as well?
Thanks a lot, any help is appreciated

How can I restrict users not to create new posts - Wordpress

When someone click on Add New post then a message will appear like "you are not allowed to create posts" and users also not allow to publish or draft the post.
I tried this code -:
function post_published_limit( $ID, $post ) {
$max_posts = 5; // change this or set it as an option that you can retrieve.
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post'); // get author post count
if ( $count > $max_posts ) {
// count too high, let's set it to draft.
$post->post_status = 'draft';
wp_update_post( $post);
}
}
add_action( 'publish_post', 'post_published_limit', 10, 2 );
This code helps me not to publish the post but it creates draft of that posts .
I don't quite understand what you mean but I think this will do the trick.
function post_published_limit( $ID, $post ) {
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "administrator" ) ){
$max_posts = 5;
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post');
if ( $count > $max_posts ) {
$post->post_status = 'draft';
wp_update_post( $post);
}
}
}

Redirect to home page after form processing with Elementor Forms API (Wordpress)

I have a multi step form built with Elementor in my Wordpress application. I am using the Elementor Forms API in PHP to grab data submitted by the form and create a custom post within Wordpress (5.7.1). Once the form has been successfully submitted I want to redirect to another page in the application.
I have been trying to use wp_redirect to do this but all I get is an error reported by the form (it only shows x parse error on the page). The code I am having trouble with is this part at the very end (everything else is working fine):-
//redirect after successful form submission
wp_redirect( home_url() );
exit();
I have tried different values for the wp_redirect argument even hard coding a URL but I still get the same issue.
Could anyone please give me a pointer to where I am going wrong??
The code is quite large but I thought it better to include it for completeness below:-
// Create a custom post and populate custom fields from a standard Elementor form
add_action( 'elementor_pro/forms/new_record', function( $record, $handler ) {
//make sure its our form
$form_name = $record->get_form_settings( 'form_name' );
if ( 'Member_Details_Form' !== $form_name ) {
return;
}
// iterate all the fields and get them into an array
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
// assign each field to its own variable - member details
$address = $fields['member_address'];
$postcode = $fields ['member_post_code'];
$contact_number = $fields ['contact_number'];
$date_of_birth = $fields ['date_of_birth'];
// assign each field to its own variable - emergency contact details
$full_name = $fields ['full_name'];
$relationship = $fields['relationship'];
$emergency_address = $fields['address'];
$post_code = $fields['post_code'];
$telephone_number = $fields['telephone_number'];
// assign each field to its own variable - courses
$courses = $fields['courses'];
// assign each field to its own variable - sign off box
$consent_for_marketing = $fields['consent_for_marketing'];
$date = $fields['date'];
//check if a current members_details post exists - only ever want just one
$user_id = get_current_user_id(); //the logged in user's id
$post_type = 'member_details';
$posts = count_user_posts( $user_id, $post_type ); //count user's posts
//if no current members_details post exist create a new one
if( $posts < 1 ){
//user has no posts so we will create a new one
global $user_ID;
$new_post = array(
'post_title' => $name,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'member_details',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
//change the title of the post to the current logged in user
set_title($post_id);
}
//make sure we have the latest post_id
$latest_cpt = get_posts("post_type=member_details&numberposts=1");
$post_id = $latest_cpt[0]->ID;
//change the title of the post to the current logged in user
set_title($post_id);
//populate some custom fields on the custom post from the Elementor form data
// add member address details.
$field_key = "member_address";// map this to an elementor field
$value = $address;
update_field( $field_key, $value, $post_id );
// add member post code.
$field_key = "member_post_code";// map this to an elementor field
$value = $postcode;
update_field( $field_key, $value, $post_id );
// add member contact number
$field_key = "contact_number";// map this to an elementor field
$value = $contact_number;
update_field( $field_key, $value, $post_id );
// add member date_of_birth
$field_key = "date_of_birth";// map this to an elementor field
//$date_of_birth = new DateTime($date_of_birth);
$value = $date_of_birth;
update_field( $field_key, $value, $post_id );
// add emergency contact name
$field_key = "full_name";// map this to an elementor field
$value = $full_name;
update_field( $field_key, $value, $post_id ); // map this to an elementor field
// Save a checkbox or select value.
$field_key = "relationship";
$value = array($relationship);
update_field( $field_key, $value, $post_id );
// add emergency contact address details.
$field_key = "address";// map this to an elementor field
$value = $emergency_address;
update_field( $field_key, $value, $post_id );
// add emergency contact post code.
$field_key = "post_code";// map this to an elementor field
$value = $post_code;
update_field( $field_key, $value, $post_id );
// add emergency contact contact number
$field_key = "telephone_number";// map this to an elementor field
$value = $telephone_number;
update_field( $field_key, $value, $post_id );
// add info on courses they are interested in
$field_key = "courses";
$value = array($courses);
update_field( $field_key, $value, $post_id );
// add consent for marketing
$field_key = "consent_for_marketing";
$value = array($consent_for_marketing);
update_field( $field_key, $value, $post_id );
// add signature date
$field_key = "date";// map this to an elementor field
$value = $date;
update_field( $field_key, $value, $post_id );
//since a post exists make sure the user role is set to member
$current_user = wp_get_current_user();
$wp_user_object = new WP_User($current_user->ID);
$wp_user_object->add_role( 'member' );
//redirect after successful form submission
wp_redirect( home_url() );
exit();
}, 10, 2 ;
You need to do it the Elementor way with record and handler.
Below you can find a general code to be used by anybody for an Elementor redirect:
//redirect URL to be setup
$redirect_url = '/after-form?encoding='.base64_encode($fields['email']);
//add to record the redirect URL
$redirect_to = $record->replace_setting_shortcodes( $redirect_url );
// Set redirect action to handler
$handler->add_response_data( 'redirect_url', $redirect_to );
In the example above the form is redirected with an encoding in the GET parameter.

Creating a wordpress virtual page with a function and page template

I'm trying to create a virtual contact page on wordpress. All the necessary data will be stored in a page template. But in my function I don't know how to link to the file.
When the user does to domain.com/contact I would like to redirect him to the contact.php file.
So far I got this function to work and show content from this line $post->post_content = 'page template: contact.php'; but I would like to show the content from templates/contact.php
add_filter( 'the_posts', 'generate_contact_page', -10 );
function generate_contact_page( $posts ) {
global $wp, $wp_query;
$url_slug = 'contact'; // URL slug of the contact page
if ( ! defined( 'CONTACT_PAGE' ) && ( strtolower( $wp->request ) == $url_slug ) ) {
// stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
define( 'CONTACT_PAGE', true );
// create a contact virtual page
$post = new stdClass;
$post->post_author = 1;
$post->post_name = $url_slug;
$post->guid = home_url() . '/' . $url_slug;
$post->post_title = 'Contact page';
$post->post_content = 'page template: contact.php';
$post->ID = -999;
$post->post_type = 'page';
$post->post_status = 'static';
$post->comment_status = 'closed';
$post->ping_status = 'open';
$post->comment_count = 0;
$post->post_date = current_time( 'mysql' );
$post->post_date_gmt = current_time( 'mysql', 1 );
$posts = NULL;
$posts[] = $post;
// make wpQuery believe this is a real page too
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
unset( $wp_query->query[ 'error' ] );
$wp_query->query_vars[ 'error' ] = '';
$wp_query->is_404 = false;
}
return $posts;
}
How can I achieve something like this? Maybe you know a better solution ?
So for template output you can try this function
function get_template_output($slug = '', $name = '') {
ob_start();
get_template_part($lug, $name);
return ob_get_clean();
}
And then to assign content you will use
$content = get_template_output('templates/', 'contact');

Wordpress Schedule post function not working?

I am working on a plugin that sent an email when a post is published using
add_action('save_post','my_function');
my_function($post_id)
{
//do everything here
}
its working fine whenever a new post is published or its being updated from quick edit,
but the problem is that its not working when a post is schedule for future publish, for this I googled it, and find the following
add_action('publish_future_post', 'my_function');
this is the same function as used for above action,
I also found following action on some results,
add_action('future_to_publish', 'my_function');
but the last 2 action not working , mean its not sending any email,
can anyone help me to figure it out ,
#Andrew Bartel
here is my complete function,
function my_function($post_id) {
$post= get_post($post_id);
if ($post->post_type == 'post' && $post->post_status == 'publish') {
global $current_user;
get_currentuserinfo();
$usernamme = $current_user->user_login;
$email= $current_user->user_email;
$fname = $current_user->user_firstname;
$lname = $current_user->user_lastname;
$disname = $current_user->display_name;
$id = $current_user->ID;
$user = new WP_User($id);
if ( !empty( $user->roles ) && is_array( $user->roles ) )
{
foreach ( $user->roles as $role )
$user_role = $role;
$upper = ucfirst($user_role);
}
$email_post_options = get_option('email_post_options');
$adminemail =(!empty($email_post_options['adminemail'])) ? $email_post_options['adminemail'] : get_bloginfo('admin_email');
if(isset($email_post_options['rol']))
{
$msg = '';
$postdet = get_post($post_id);
$title = $postdet->post_title;
//$excerpt = substr($postdet->post_content,0,150);
$pdate = $postdet->post_date;
$permalink = get_permalink($post_id);
$price = get_post_meta( $post_id, '_my_meta_value_key', true );
$date = get_post_meta( $post_id, '_my_meta_date_key', true );
foreach($email_post_options['rol'] as $mailrol) // the roles which are saved from the plugin settings page, which is telling that who's role email will be received when a new post from the user is created.
{
if($mailrol==$upper)
{
$name = $fname.' '.$lname;
$usename = ($name!=' ')? $name : $usernamme;
$msg .='Full Name / Username : ' .$usename."\n";
$msg .='Title : '.$title."\n";
//$msg .='<p>Content : '.$excerpt.'</p>';
$msg .='Link = '.$permalink."\n";
$msg .='Price is = '.$price."\n";
$msg .='Added date = '.$date."\n";
$msg .='Published date = '.$pdate."\n";
$msg .='Total Posts : '.count_user_posts($id)."\n";
echo $msg;
if($email_post_options['npemail']==1)
{
wp_mail($adminemail, 'New Post', $msg);
}
}
}
}
} // end if
} // end function
its my function , if you have any confusion in this please let me know.
On line 3 you're checking the post_status of the post and explicitly checking for publish, which is only set for posts that (you guessed it) are published. When a post is scheduled to be published later, it's status is set to future. For your example, the first three lines:
function my_function($post_id) {
$post= get_post($post_id);
if ($post->post_type == 'post' && ($post->post_status == 'publish' || $post->post_status == 'future') ) {
Let me know if that works for you.

Categories