ACF Custom fields, and Wordpress Timber front end forms - wordpress

I'm trying to create a custom form on the front end so that logged in visitors can post their own content.
Using ACF pro and Timber.
In my page.php file I have
$new_post = array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'field_groups' => array(26), // Create post field group ID(s)
'form' => true,
'return' => '%post_url%', // Redirect to new post url
'html_before_fields' => '<div class="foobar">',
'html_after_fields' => '</div>',
'new_post' => array(
'post_status' => 'draft',
'post_type' => 'vlog'
),
'submit_value' => 'Create Post'
);
$context['vlogform'] = acf_form($new_post);
and in page.twig, I have placed
{{vlogform}}
The form renders - but its stuck in a silly place, not where I want it. Its stuck above the html element...
Any pointers?
Thanks,
Rob
** Edit **
This doesn't work
$context['vf_form'] = Timber\Helper::ob_function('acf_form', $new_post);
but this does...
ob_start();
acf_Form($new_post);
$context['vf_form'] = ob_get_clean();
Materially, what is the difference?
Thanks,
Rob

This might be a little late, but I've managed to fix this issue by using an argument that they have added recently. I found this googling for it, so for future seekers this might come in handy.
So, in the args array, add the following:
$args = array(
'echo' => false,
);

So as other answers have mentioned acf_form() outputs the form when it is called.
To prevent the form from rendering at the top of the document, pass over your form arguments from the controller (page.php) to the view
acf_form_head();
get_header();
$context = Timber::context();
$context['post'] = new TimberPost();
$context['form_args'] = [
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'field_groups' => [26], // Create post field group ID(s)
'form' => true,
'return' => '%post_url%', // Redirect to new post url
'html_before_fields' => '<div class="foobar">',
'html_after_fields' => '</div>',
'new_post' => [
'post_status' => 'draft',
'post_type' => 'vlog'
],
'submit_value' => 'Create Post'
];
Timber::render('example-view.twig', $context);
get_footer();
Then in your view, use timbers function()helper method to call acf_form()
{{ function('acf_form', form_args) }}
src: https://timber.github.io/docs/guides/functions/#function
I am using latest Timber (via composer) and ACF Pro 5.9.1
On older versions of Timber, I took advantage of a similar helper method (TimberHelper::function) in the controller which is now deprecated

The problem is that acf_form is also echoing when called in the PHP file. You can either look for an alternate function (is there something like get_acf_form that returns the HTML but doesn't echo?) OR use Timber's output buffer wrapper:
$contect['vf_form'] = Timber\Helper::ob_function('acf_form', $new_post);
... this will store the data in the vf_form attribute, but not suppress it from echoing until called in Twig

Related

Creating pages when activating plugin WordPress

Anyone Please help!
I have a plugin which creates post_type pages in the backend. The plugin is creating the desired pages but the problem is whenever i try to see the page list, it shows "No pages found" message. Screenshot here: http://prnt.sc/azalub
My code for creating the required pages here:
$new_page = array('post_title' => $title,
'post_content' => '['.$shortcode.']',
'post_status' => 'publish',
'post_type' => 'page'
);
$post_id = wp_insert_post( $new_page );
For this purpose, you need to register with plugin activation hook.
See the code example below:
function add_my_custom_page() {
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( 'My Custom Page' ),
'post_content' => 'My custom page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, 'add_my_custom_page');
While creating a custom post type, i had set 'query_var' to 'true' on one of the custom post type in my plugin. Setting it 'false' just made everything fine.
if you are using class try pass array($this, 'method_name') instead of function name.

Unable to edit a new Custom Taxonomy in Wordpress

I have just registered a new Custom Taxonomy in Wordpress (as per the docs). Here is a copy of the code in functions.php for reference:
function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides'
)
)
);
}
add_action( 'init', 'people_init' );
As you can see in the image below, the Taxonomy appears within left-hand navigation but when my (admin) user clicks on the option I am displayed with an You are not allowed to edit this item. error:
Can anyone suggest why this may be?
Almost as soon as I posted this I realised it is the capabilities array. Removing this, so that it reverts to the default allows access as intended.
After further investigation I found that the following was the best settings to get this functioning correctly:
'capabilities' => array(
'manage__terms' => 'edit_posts',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts'
)

Getting wp_list_categories() custom walker to paginate

I've got a custom post type registered, with custom taxonomies, everything good and clear.
I wish I could somehow display all the categories of the taxonomy with pagination.
I am using a custom category Walker and am thinking to register a custom page rewrite for page query, and add some code to the category walker to display only the desired interval. Am I on the right direction?
Also, wp_list_categories sends to the category Walker the entire list of categories. Is there any way to get only the desired interval?
In this particular setup, no.
But I found a workaround: I registered a function that created a custom-type post each time I added a category in the taxonomy. That way I used the archive feature for custom post types, made available in 3.1-RC1.
function create_crew_post_on_term($term_id) {
$term = get_term($term_id, 'crew');
$post = array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_author' => 1,
'post_content' => '',
'post_date' => date('Y-m-d H:i:s'),
'post_excerpt' => '',
'post_name' => $term->slug,
'post_status' => 'publish',
'post_title' => $term->name,
'post_type' => 'crew'
);
wp_insert_post( $post );
}
add_action('created_crew', 'create_crew_post_on_term');

WYSIWYG editor in texarea for Drupal configuration form

Is it possible to use a WYSIWYG editor in texarea
for Drupal site configuration form (system_settings_form).
This is how the configuration is coded now...
$form['my_module_text_bottom'] = array(
'#type' => 'textarea',
'#title' => t('Some text'),
'#default_value' => variable_get('my_module_text_bottom', 'This is configurable text found in the module configuration.'),
'#size' => 1024,
'#maxlength' => 1024,
'#description' => t("Some text."),
'#required' => TRUE,
);
return system_settings_form($form);
Here it is for Drupal 7 and Drupal 6.
For D7:
<?php
// Retrieve the default values for 'value' and 'format', if not readily
// available through other means:
$defaults = array(
'value' => '',
'format' => filter_default_format(),
);
$my_richtext_field = variable_get('my_richtext_field', $defaults);
// Just construct a regular #type 'text_format' form element:
$form['my_richtext_field'] = array(
'#type' => 'text_format',
'#title' => t('My richtext field'),
'#default_value' => $my_richtext_field['value'],
'#format' => $my_richtext_field['format'],
);
?>
For D6:
<?php
// Your saved or new data is supposed to have a value and a format. Just like
// $node has a $node->body and $node->format. May also come from a
// variable_get('mymodule_admin_setting', array('value' => '', 'format' => NULL));
$mydata = mymodule_data_load();
$form['myfield']['mytextarea'] = array(
'#type' => 'textarea',
'#title' => t('My textarea'),
'#default_value' => $mydata->value,
);
$form['myfield']['format'] = filter_form($mydata->format);
?>
I kept searching for this issue for about 6 hours and finally i found the reason, for your custom textarea field you must add this line, to use the default input format (Full HTML):
$form['format'] = filter_form();
be careful if you use this form element inside fieldset you must include this fieldset:
$form['donation-instructions']['format'] = filter_form();
I hope this will help you
The WYSIWYG or CKEditor modules should be able to do this.
I found this question similar to:
Drupal 6: Implement Wysiwyg on Custom Module Form
One of the answers there pointed to this drupal.org page:
http://drupal.org/node/358316
which provides fairly detailed examples of the "format" array key and filter_form(), also describing how it's used if your form has multiple textareas.
The approach given there doesn't apply to Drupal 7.
I ran into a similar situation where I'd downloaded and installed and installed CKEditor and it displayed when editing content nodes, but didn't display for the textarea on a configuration form for my module.

Wordpress blog setup script to create about, contact, privacy pages

I create lots of blogs that all share the same basic structure of a single "post" and 3 "pages" (about us, contact us, privacy). For the "post" page, I usually just edit the existing "hello world" post once I'm in the newly created blog. For the pages, I start with the "about" page and just edit that for my "about us" page.
I'd like to create a script or plug-in that I can simply add to my site that will, when executed, automatically create those pages with my boilerplate content. For the "about us" page, I can just edit the default "about page" via script. And for the initial "post" the script can just edit the "hello world" post. The other "pages" will need to be created from scratch via the script.
I'm looking for ideas on how best to do it (a plug-in or a script thats uploaded and executed) along with some help with the wordpress API calls used to create and update a post and a page's name and content.
For the default content, the about and contact content will be very short but I will need to use an external file to populate the privacy page. I'm thinking that a preformatted .html file should be used as the basis of this database insert.
Thanks for your help!
If you set up the blogs from scratch each time, you could manipulate /wp-admin/includes/upgrade.php (function wp_install_defaults()) and put your pages there.
That is, however, if you are comfortable with hacking WP core files.
Another possibility is to do it after installation with a custom SQL script. Just use phpMyAdmin to look what a current page looks like and create your own "INSERT INTO" statement. For convenience, you could write a small WP plugin, that does the insert on activation and afterwards removes itself, or something like this.
Edit: From looking at the above mentioned file, you can do everything from within PHP with this:
$wpdb->insert( $wpdb->posts, array(
'post_author' => $user_id,
'post_date' => $now,
'post_date_gmt' => $now_gmt,
'post_content' => __('Lorem ipsum...'),
'post_excerpt' => '',
'post_title' => __('About'),
/* translators: Default page slug */
'post_name' => _x('about', 'Default page slug'),
'post_modified' => $now,
'post_modified_gmt' => $now_gmt,
'guid' => $first_post_guid,
'post_type' => 'page',
'to_ping' => '',
'pinged' => '',
'post_content_filtered' => ''
));
You could put it in a plugin you just upload, activate and delete.
Here's what I use, modified for your use (havent tested it):
<?php
/*
Plugin Name: Startup Settings
Plugin URI: http://someurl/
Description: Some description
Version: 1
Author: Your Name
Author URI: http://yoursite.com
*/
function startup_settings()
{
// Remove "Hello world" post and comment.
wp_delete_post(1, TRUE);
wp_delete_comment(1);
// Insert your pages.
global $user_ID;
$about_page = array(
'post_title' => 'About us',
'post_content' => 'Your content goes here',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
'post_category' => array(0)
);
$post_id = wp_insert_post($about_page);
$contact_page = array(
'post_title' => 'Contact us',
'post_content' => 'Your content goes here',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
'post_category' => array(0)
);
$post_id = wp_insert_post($contact_page);
// if you want to load the privacy text from a external file (untested)
$myFile = "/path/to/privacy.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$privacy_page = array(
'post_title' => 'Privacy',
'post_content' => $theData,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
'post_category' => array(0)
);
$post_id = wp_insert_post($privacy_page);
// Set some deafault options if you want.
$options = array(
'comment_max_links' => 0,
'comments_per_page' => 0,
'date_format' => 'd.m.Y',
'default_ping_status' => 'closed',
'links_updated_date_format' => 'l, F j, Y',
'permalink_structure' => '/%postname%/',
'rss_language' => 'en',
'use_smilies' => 0
);
foreach ( $options as $option => $value )
{
update_option($option, $value);
}
return;
}
register_activation_hook(__FILE__, 'startup_settings');
?>

Resources