ACF field to create sets of pages when saved - wordpress

I have an odd request, i'm not sure if this is even possible. But i'll try to work out the process below, and if anyone can help me work this out that would be amazing!
Ideally the process is as follows:
Admin goes to parent options page, within the options page there is a repeater field, called add new company. This will just be a field with a title.
Admin fills in field and presses save. This will generate a sub options page with that name, within the options field, there will be a set of fields like logo, a colour picker and some text fields (these could be a set of fields from within ACF if thats possible).
Also when this original Repeater Field is made/saved a set of pages is generated from a set of templates. Essentially using the name from the repeater field to be the main page title for the top level page and all the sub pages below are just dynamically generated. They don't need to have anything different about them, they just need to generate from a set of page templates. It needs to be able to associate with the newly generated company bits from the sub options field.
This will then essentially give the admin a new set of pages which will use the new options logo / colours etc. It would almost need to generate a new set of templates based off the master templates to dynamically make sure it picked up the correct information from the sub options page.
I'm not sure if this is possible, I have seen it work elsewhere on another job I have worked on (not exactly the same as the above but similar), but I can't work out the process to make it work sadly, as I have a horrid feeling that there is some complex bits within the database going on to do the duplication dynamically.
My other option is to run everything as a WordPress Multisite but I was trying to avoid that if possible on this occasion, but I may have to use Multisite to achieve the above.
If anyone can help me work this out that would be amazing!
Thanks in advance for any help :)

You should be able to plug into the save_post action and create new subpages from there.
add_action( 'save_post', 'create_sub_pages' ); //Plug into save_post action
//Function create sub_pages
function create_sub_pages($post_ID) {
//Repeater field name
$repeater_field_array = get_field('repeater_field_name');
//Loops through all of the items in the repeater field
foreach($repeater_field_array as $key => $value) {
//Check to see if there is already a sub page with that post name
$child_pages = get_pages(array( 'child_of' => $post_ID ));
$child_page_exists = false;
foreach($child_pages as $pages) {
if ($pages->post_title === $key) {
$child_page_exists = true;
}
}
//If not, set up the creation of the new post
if ($child_page_exists === false) {
$new_page_title = esc_html__( $key );
$new_page_content = '';
$new_page = array(
'post_type' => 'page',
'post_date' => esc_attr( date('Y-m-d H:i:s', time()) ),
'post_date_gmt' => esc_attr( date('Y-m-d H:i:s', time()) ),
'post_title' => esc_attr( $new_page_title ),
'post_name' => sanitize_title( $new_page_title ), //This could from the sub_field in the repeater
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_parent' => $post_ID,
'menu_order' => $new_page_order
);
$new_page_id = wp_insert_post( $new_page );
update_post_meta( $new_page_id, '_wp_page_template', $value );
}
}
}
Again, this is just a spitball since there was not much code to review, but it could help you get going in the right direction.

Related

Programmatically construct a dynamic dropdown field in Contact Form 7

I looked around here and by search engine, but unfortunately I couldn't find a solution for myself.
So, I now ask assistance with a function that I need to customize for the Contact Form 7 WordPress plugin. The function was from another question.
In a drop-down menu (select) I need two details (workshop name and date) in one option field. Both details come from the same post of a custom post type. The first detail is a post_title, the second is a custom-field from Meta-Box plugin.
The following function works in principle, but it only returns the one or the other detail. Probably the solution is within the foreach construct. But I don't know how it works.
I would be very grateful for support!
[UPDATE 2018-08-12]
After further research, I've found the solution at this post and changed the function accordingly.
The solution should look like this:
<select>
<option value="workshop name – date">workshop name – date</option>
...
</select>
This is the function:
add_filter( 'wpcf7_form_tag', 'dynamic_field_choose_workshop', 10, 2);
function dynamic_field_choose_workshop ( $tag, $unused ) {
if ( $tag['name'] != 'workshop' )
return $tag;
$args = array (
'post_type' => 'workshop',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'numberposts' => - 1,
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$ID = $custom_post->ID;
$tag['values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
$tag['raw_values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
$tag['labels'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
}
return $tag;
}
There is CF7 extension that will do this for you. Checkout the Smart Grid-Layout for CF7, it introduces a new tag called dynamic_dropdown. This is is what you want to use. The dynamic_dropdown creates a select field and allows you to populate the field options using either a taxonomy, titles of a post type, or a filter. You want to use the filter option to actually construct the options as per your requirement. The tag popup window is self explanatory, however if you get stuck post a comment below and I'll give you some more tips.
Using the following dynamic_dropdown tag,
[dynamic_select workshop-date-select class:select2 "source:filter"]
it creates a <select name="workshop-date-select"> dropdown field which will be converted into a select2 jquery field on the front end, and its values dynamically created using the following function placed in the functions.php file,
add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
/*first we verify if this is the right field from the right form
in case multiple forms with similar fieldd exiss.
the $form_key is a unique key exposed by the Smart Grid-layout plugin
instead of using form IDs to make forms and code more portable across servers.*/
if($form_key != 'my-form' && $field_name != 'workshop-date-select') return $options;
$options = array();
//load your options programmatically, as $value=>$name pairs.
$args = array (
'post_type' => 'workshop',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'numberposts' => - 1,
);
$workshops = get_posts( $args );
foreach($workshops as $workshop){
$val = $workshop->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $workshop->ID);
$options[$val]=$val;
}
return $options;
}
this will create the desired dropdown select field in the front end.
NOTE of CAUTION: I would populate the option values as the workshop post ID rather than the same text as the option label. When the form is submitted the value of the post ID can be used to populate the notification email with the desired workshop title and date. This gives more flexibility to expand the reported information in the future.

ACF Front Forms and Multsite

I have a 2x blog WordPress Multisite that contains a front end ACF form for adding dates.
I can then update these dates whenever required from the front end.
I want to load the front end form on the second blog so that I can also update the dates there.
I can load the form but the values aren't saved; whereas they are only the main blog site.
How do I get around this?
<?php
$options = array(
'id' => 'update_dates_form',
'fields' => array(
'field_591c745961034',
'field_591c74a861037',
'field_591c748361035',
'field_591c749a61036',
),
'submit_value' => __('Save', 'augwp'),
'updated_message' => __('Successfully Updated', 'augwp'),
);
acf_form( $options );
?>
In your code, yiu have to switch to blog where data are stored, extract them then return to the current blog:
switch_to_blog(1); // use blog number here, main blog is always 1
// extract data
restore_current_blog();

Event Espresso Query Modification?

I am using Event Espresso with WordPress.
May u help me out in further query modification?
Hope you will :)
I want to use meta_query to list events on page.
Somewhat like below code.
$atts = array(
'title' => NULL,
'limit' => 10,
'css_class' => NULL,
'show_expired' => FALSE,
'month' => NULL,
'category_slug' => NULL,
'order_by' => 'start_date',
//'order_by' => 'end_date',
'sort' => 'DESC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => '2017-01-08 08:00:00',
'type' => 'DATETIME',
'compare' => '>=',
),
)
);
I want to implement search functionality for Event Espresso and i have those fields:
State - Dropdown (How to list all state? May be Venue)
Category - Dropdown
Start Date - Datepicker
End Date - Datepicker
Keyword - input
On submit those values will be submitted and based on those , i will get filtered events that are related with those values.
So how to implement this?
Please help.
Thanks in Advance
A slightly delayed response to this...
First, this depends on the version of EE you're using. They support both EE3 and EE4. I use EE4, so any reference to code I make is specific to version 4.
In order to create an events archive with a filtering functionality, you'd need to use the EE events archive. EE uses custom database tables and a lot of different post types to achieve what you see, so creating a simple archive with these filters won't work very well. Very little is stored in the _posts and _postmeta tables and you need to get post meta from related post types and tables that aren't laid out like WP_Query likes. They have a shortcode for the events list, which is laid out here and has a lot of the filters you're looking for, but no search functionality.
Their support forum has a lot of snippets and such created by their staff and there's a (long) related post about formatting the events archive page here. You'll also need to go through this documentation to see about their custom methods and hooks.
You could copy over and modify the archive templates they describe in a child theme to add search functionality to the top of the page. This would allow you to directly override the archive. You'll need to make use of some fancy filters, which are covered pretty clearly over on WPMU Dev.
The pieces of information you're looking for for filtering are here, minus keyword:
<?php
if( have_posts() ){
while ( have_posts() ){ the_post(); // enter the WordPress loop
$id = get_the_ID(); // get the ID of the current post in the loop, post ID = EVT_ID
$terms = get_the_terms( $id, 'espresso_event_categories' ); // get the event categories for the current post
if ( $terms && ! is_wp_error( $terms ) ) {
$cats = array();
foreach ( $terms as $term ) {
// do something
}
}
$event = EEM_Event::instance()->get_one_by_ID( $id ); // get the event OBJECT using EE's existing method
$venue = $event->venue(); // get the venue object for the current event
$state = $venue instanceof EE_Venue ? $venue->state_abbrev(); // get the event venue's state, but you can use state_name() to get the full name
// using the event to get the first and last date for the entire event. Sub $datetime for $event to do it per datetime
$start_date = $event->start_date('j M Y');
$end_date = $event->end_date('j M Y');
$start_time = $event->start_time(get_option('time_format'));
$end_time = $event->end_time(get_option('time_format'));
?>
<!-- Do some awesome layout stuff here -->
<?php
}
}
espresso_pagination();
?>
This will give you the meta for each post within the loop as variables, but you may want to pull them into pre_get_posts. You can just as easily create an array of $events and then filter it using the variables.
I'm not sure what your exact needs are regarding keyword. Do you mean tags? Title keywords? Description search? You'll need to narrow down exactly what you need this to do in order to write a function for it.

Wordpress Profile File / Page

I would like to create a file in the Wordpress theme, where i will add my own code, edit profile, show profile information, and perhaps an ability to insert posts / meta data programmatically.
So it needs to be www.mysite.com/profile.php or www.mysite.com/profile/
I do not want to use Buddy Press or any other plugin.
I know how the template system works, i do not want a page template.
It will probably be a class, later on, i do not want to change .htaccess file, and if i must i would appreciated filter function how to do this from functions.php
Basically just a simple .php file i can link to, located in theme root.
include('../../../wp-load.php');
and write any code i would like to.
Any creative solution that is not too "hacky" would be appreciated.
Spent around 2 days googling bashing my head on this, before i decided to ask question.
Thank you very much.
Ok, I managed to do this, took me 2 days to figure it out. Here is how I managed to do it:
Make a plugin folder.
In that plugin folder make 1x php file. so index.php
Ok so first thing we need to register plugin I did it like this, in your index.php paste
this code.
function activate_profile_plugin() {
add_option( 'Activated_Plugin', 'Plugin-Slug' );
/* activation code here */
}
register_activation_hook( __FILE__, 'activate_profile_plugin' );
Then we need a function when you register a plugin only once register profile pages.
function create_profile_page( $title, $slug, $post_type, $shortcode, $template = null ) {
//Check if the page with this name exists.
if(!get_page_by_title($title)) {
// if not :
$page_id = -1;
$page_id = wp_insert_post(
array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_content' => $shortcode,
'post_author' => 1, // Administrator is creating the page
'post_title' => $title,
'post_name' => strtolower( $slug ),
'post_status' => 'publish',
'post_type' => strtolower( $post_type )
)
);
// If a template is specified in the function arguments, let's apply it
if( null != $template ) {
update_post_meta( get_the_ID(), '_wp_page_template', $template );
} // end if
return $page_id;
}
}
Ok so we created function which programatically register pages. It has 5 paramethers.
is Title
Slug
Post type
Shortcode.
Template
For the shortcode template you need to make a shortcode with the complete page output
and add it as a parameter to this function, so for registration page it will be a shortcode with the registration forms etc.
For example :
function registration_shortcode(){
echo 'Wellcome to Registration page';
}
add_shortcode('registration_output', 'registration_shortcode');
Next thing we need to call it once only when plugin loads.
so we do this :
function load_plugin() {
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
delete_option( 'Activated_Plugin' );
/* do stuff once right after activation */
// example: add_action( 'init', 'my_init_function' );
create_profile_page('Registration', 'registration', 'page', '[registration_output]');
create_profile_page('Profile', 'profile', 'page', '[profile_shortcode]');
create_profile_page('Profil Edit', 'profile-edit', 'page', '[edit_shortcode]');
}
}
add_action( 'admin_init', 'load_plugin' );
Ok so this will execute only once when plugin loads and it will create 3 Pages, which are Profile, Registration and Profile Edit.
And that's it, you have your front-end user profile blank pages, and you can write page output in shortcodes ,create more pages, put any forms or elements you like and create decent profile (which doesn't have any stuff you don't need in it like plugins. )
Hope this helps, it was painful for me to figure this out. Cheers!

Adding postmeta default values after adding post

I am using Wordpress XMLRPC to add posts to my blog.
However, after running this.
$data = array(
'title' => $title,
'description' => $content,
'post_type' => 'post',
'categories' => array($category),
'post_status' => 'publish'
);
$addedPostReturn = $this->_client->query('metaWeblog.newPost',
array(0,$username,$password,$data,1));
This adds the post fine but doesn't add the postmeta information.
If i open the post, click Update, all the default postmeta gets updated. However, I would like to add all the default postmeta information with my php script instead of manually (or else it kinda defeats the purpose).
Is there anyway, either with xmlrpc or regular wordpress functions to create the postmeta custom fields by using their default values? If not, is there a way to have the list of all the custom fields i need to manually add using the custom fields section of the metaWeblog.newPost function? (I don't want to add some but not others. I rather do a complete job)
Thanks in advance... any help is appreciated! :)
EDIT:
$postUpdateContent = array();
$postUpdateContent['ID'] = $newPostId;
$postUpdateContent['post_content'] = $sameContent;
wp_update_post( $postUpdateContent );
didn't seem to do the trick...

Resources