I am using a Toolset plugin for Wordpress and try to make it save the id of the page where the user fills up the form.
This plugin has "hooks" for it.
This is what they say about it.
cred_save_data
Description
This hook allows doing a custom action when post data is saved to the database.
Arguments
post_id. The id of the current created or edited post.
form_data. An associative array of data about current form:
id. The form of ID.
post_type. The post type that the form operates on.
form_type. The type of form - 'new' or 'edit'.
container_id. Refers to the post_id of the post, page or custom post type that contains the CRED form
EXAMPLE:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
{
if (isset($_POST['my_custom_field']))
{
// add it to saved post meta
add_post_meta($post_id, '__my_custom_field', $_POST['my_custom_field'], true);
}
}
}
so I added this modified PHP code in functions.php of the theme:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==2080) // the id of the form
{
if (isset($_POST['container_id']))
{
// add it to saved post meta
add_post_meta($post_id, 'formetadata', $_POST['container_id'], true);
}
}
}
where for metadata is a slug for custom field created to collect metadata, including the id of the page, where the form was filled up.
It does not work. The form collects data, creates a post, but the field for metadata remains empty.
What did I do wrong?
I don't have enough knowledge about PHP and may be completely misunderstood instructions...
So looking at the docs and your function I see they use the key container_id and you use $_POST['container_id']. Im just guessing but it sounds like those are the same id? If so then you can access it via the $form_data like below:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==2080) // the id of the form
{
if (isset($_POST['container_id']))
{
// add it to saved post meta
add_post_meta($post_id, 'formetadata', $form_data['container_id'], true);
}
}
}
Related
I have created a configuration form in Grav's admin panel, and I want to extend/modify some of it's values on save.
More precisely, I have a list form element that looks like this in the blueprint:
topics:
type: list
fields:
.name:
type: text
.unique_id:
type: text
readonly: true
default: generate_on_save
On save, I want to replace all generate_on_save values with a unique id.
I tried to hook into the onAdminSave Event, but the Event Object contained just an instance of \Grav\Common\Data\Blueprint and no actual form data. I then tried to modify the request object, but when I register the modified request in the grav container, I get an error Cannot override frozen service 'request'.
How can I accomplish this task?
I did the following which works fine:
In config file /user/themes/quark/blueprints.yaml, I copied your field definition.
In Admin I added some topics on the config page of theme Quark.
The 'Save' action was captured by the following eventhandler:
public function onAdminSave(Event $event) {
/** #var Data */
$form = $event['object'];
$topics = $form['topics'];
foreach ($topics as &$topic) {
if ($topic['unique_id'] === 'generate_on_save') {
$topic['unique_id'] = str_rot13($topic['name']);
}
}
// Note: Updated $form['topics'] must be re-assigned
$form['topics'] = $topics;
}
The topics with there "unique" values have correctly been written to /user/config/themes/quark.yaml
How can I modify a Wordpress theme to auto-populate the header image of a Post if one hasn't been set manually? I'm a complete newbie when it comes to PHP.
Using get_post_meta() function you can achieve this functionality. You
can pass post id in first parameter , you can use custom_field_name in
second. And check like below code that it's available in database or
not.
Try this code.
<?php
if(is_single() & !is_home())
{
$myfield = 'custom_field_name'; // Change this to the name of the custom field you use..
$postimage = get_post_meta($post->ID, $myfield, true);
if($postimage)
{
// If the field has a value.. set image path using value...
}
elseif(!$postimage)
{
// If no value than set Default image path..
}
}
?>
Is there a way to create/update form poll fields based on a content type? For example, I've got a post type called Candidate and I'd like like to dynamically update a poll list when a new Candidate is added, or when one is removed.
The reason I'm looking for this is because I'm creating a voting mechanism for this client and they have requested that users see an image, the name, and brief Bio of who they are voting for. My idea is to tie in the names so that I can target a hidden Gravity Form poll on page so when the voter clicks, it updates the corresponding named checkbox.
I can of course add each Candidate one by one, and then add each candidate one by one in the form, but was hoping there was a way to do that in code. So far I haven't found much other than filters in the Gravity Forms Documentation.
To reiterate, my question here is not the frontend connection, rather how to dynamically update/add an option in a poll field in a form when content is created for a Candidate post type.
Any help would be appreciated.
I believe the solution you're after is documented here:
http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_pre_render/
Probably a combination of solution examples #1 and #2 on that page will fit your needs? So -
Create a category of standard Wordpress pages (category name: "Candidates"), and the pages would contain the candidate information (bio, photo, etc).
In your functions.php file for your current theme, you'd add something like the following to pull out that category's worth of posts:
// modify form output for the public page
add_filter("gform_pre_render", "populate_checkbox");
// modify form in admin
add_filter("gform_admin_pre_render", "populate_checkbox");
function populate_dropdown($form) {
// some kind of logic / code to limit what form you're editing
if ($form["id"] != 1) { return $form; }
//Reading posts for "Business" category;
$posts = get_posts("category=" . get_cat_ID("Candidates"));
foreach ($form['fields'] as &$field) {
// assuming field #1, if this is a voting form that uses checkboxes
$target_field = 1;
if ($field['id'] != $target_field) { break; }
// init the counting var for how many checkboxes we'll be outputting
// there's some kind of error with checkboxes and multiples of 10?
$input_id = 1;
foreach ($posts as $post) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if($input_id % 10 == 0) { $input_id++; }
// here's where you format your field inputs as you need
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
$inputs[] = array("label" => $post->post_title, "id" => "{$field_id}.{$input_id}");
// increment the number of checkboxes for ID handling
$input_id++;
}
$field['choices'] = $choices;
$field['inputs'] = $inputs;
}
// return the updated form
return $form;
}
I have been trying to do display a custom field I created in the manage fields section of user accounts for nodes in addition to the profile page. The problem I am having with this code is that it will display the first field it finds and display that for every user, not the field for that particular user.
And ideas? I believe it's finding the first value in the array and not the value for the particular user in the array.
Here is m setup so far:
Added this to my template.php of my theme:
function mythemename_preprocess_node(&$vars) {
global $user;
$user = user_load($user->uid); // Make sure the user object is fully loaded
$team = field_get_items('user', $user, 'field_team');
if ($team) {
$vars['field_team'] = $team[0]['value'];
}
}
Then, added this to my node.tpl.php in order to display it on nodes.
if (isset($field_team) && !empty($field_team)) :
echo '$field_team.'</div>';
endif;
UPDATE:
Found my own aswer here:
http://drupal.org/node/1194506
Code used:
<?php
$node_author = user_load($node->uid);
print ($node_author->roles[3]);
print ($node_author->field_biography['und'][0]['value']);
?>
You can use drupal's 'Author Pane' module for that. Try this:
http://drupal.org/project/author_pane
My function below, will take the values from my custom meta fields (after a post has been edited, and save or publish has been clicked) and update or insert the posted meta values.
However, if the user leaves this field blank, I believe I want to delete the meta altogether (so I can test for its presence and display accordingly vs just checking for "").
For example, one of my meta options gives the user the ability to add a Custom title to their post, which when present, will populate the page's tag. However, if the field is left empty, I want to default the tag to the_title(), which is simply the Post title used to identify the page/post.
Since I'm not deleting the meta on save, its always present after the first time a user enters something in there, get_post_meta($post->ID,'MyCustomTitle', true) is always true. Further, they cannot blank it out by clearing the title field and hitting publish.
What am I missing in the save in order to clear the value to "" when the user clears the field?
if ($_POST['MyCustomTitle']) {
update_custom_meta($postID, $_POST['MyCustomTitle'], 'MyCustomTitle');
}
function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}
What about something like
$customTitle = $_POST['MyCustomTitle'];
if ($customTitle<>"") {
update_custom_meta($postID, $customTitle, 'MyCustomTitle');
}else{
$customTitle = "";
update_custom_meta($postID, $customTitle, 'MyCustomTitle');
}
that takes your post value, assigns to a variable '$customTitle'
if its NOT empty then update with the new value or else just update it with a blank value?