Wordpress: How to integrate the media uploader with a custom meta box field? - wordpress

I'm writing a plugin which creates a custom post_type called "dictionary_entry" which has several custom meta boxes and fields. I'd like to add an addition field which allows the custom post author to upload an audio clip.
I've done some digging and tried the code offered here but I can't get it to work.
I think one possible answer to my question would be the "type" parameter for fields. I've seen "text", "textarea", "time", "color", "radio", etc. but I haven't been able to find a list of all the possibilities. Is it wishful thinking that there might be a field type: "file" or "upload"?
I'm going to skip the code for adding the custom post_type, but here's an example of my code for adding the meta boxes (in case somebody else is trying to use this, remember to use your custom post_type in the 'pages' parameter):
//meta box code
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'examples', // meta box id, unique per meta box
'title' => 'Examples', // meta box title
'pages' => array('dictionary_entry'), // post types, accept custom post types as well, default is array('post'); optional
'context' => 'normal', // where the meta box appear: normal (default), advanced, side; optional
'priority' => 'high', // order of meta box: high (default), low; optional
'fields' => array( // list of meta fields
array(
'name' => 'Example 1', // field name
'desc' => 'Use it in a sentence? EX: Kanien\'kéha kahrónkha.', // field description, optional
'id' => $prefix . 'example1', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
'validate_func' => 'check_apos' // validate function, created below, inside RW_Meta_Box_Validate class
),
array(
'name' => 'Translation 1', // field name
'desc' => 'What does the sentence mean? EX: I speak Mohawk.', // field description, optional
'id' => $prefix . 'ex_translation1', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
'validate_func' => 'check_apos' // validate function, created below, inside RW_Meta_Box_Validate class
)
)
);
foreach ($meta_boxes as $metabox) {
add_meta_box... //see the codex for add_meta_box()
}

I figured out how to do this by digging into the code found here. If you take a look, you'll recognize parts of my code quoted above. I was originally using this class, but didn't fully realize it. It's a custom class which can be called to add various meta boxes / fields.
It turns out that the "type" parameter I was wondering about actually belongs to this Class (as opposed to the Wordpress API) and that it does allow for a type: 'file' which brings up a default file picker window (not the built-in media uploader). For my purposes this is okay, because I don't need all the slick options.
If you're reading this, you've probably already googled this question and seen a wide variety of posts that partially explain how to do this. For what it's worth, I found this to be the easiest way to add this functionality that ALSO works for custom post_types (without a fair amount of hacking). I hope this is useful to someone else.

Related

WooCommerce - include custom fields in the search function

So I'm looking to include the functionality of displaying content by its custom fields as well as its title and content.
I need to be able to search for orders, as well as subscriptions, on WooCommerce by custom field as well as the normal method. Is there any way I can, without adding additional search forms or booleans, simply get Wordpress to display posts that match with the search term by their custom fields, too?
I have used the following code thanks to a responder on here:
function custom_search_query( $query ) {
$custom_fields = array(
// put all the meta fields you want to search for here
"gender",
"birthdate"
);
$searchterm = $query->query_vars['s'];
// we have to remove the "s" parameter from the query, because it will prevent the posts from being found
$query->query_vars['s'] = "";
if ($searchterm != "") {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => '=='
));
}
$query->set("meta_query", $meta_query);
};
}
add_filter( "pre_get_posts", "custom_search_query");
This works great when searching orders, but what I need to search is subscriptions, where it doesn't work.
Help would be much appreciated!

How to hide Add New custom post type button

I want to hide admin side my custom type Add New button. Review this screen shot -> https://s.nimbus.everhelper.me/share/1406976/p6f5u0nlvs5xff3sr85h
So any one know solutions then please update me.
Thanks.
you can use meta capability create_posts that is not documented but is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
),
'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
Where custom_post_type_name you can put your custom post type name, which you are creating.

RSS item elements not being not being read by XML Reader

I don't know if i am asking this correctly but plz help me.
I am using Drupal Views to display RSS for my site. I am using the 'Views RSS: Core Elements' module to map the RSS fields with the custom fields added by me in the views. All works well. But a requirement forces me to add custom item elements in the RSS display. I am implementing the hook_views_rss_item_elements() to add custom item elements. When i view the RSS page i get the see the custom items but when i try to read the RSS using a RSS reader, the items are not being read. What am i doing wrong, or am i missing something?
Here is my code to add custom items to the 'SHOW ITEM ELEMENTS : CORE' for RSS display:
function Mymodule_views_rss_item_elements() {
$elements['blogs_title_image'] = array ( 'title' => t('Blogs Title Image'), 'description' => t('Blogs Title Image'), );
$elements['blogs_additional_tags'] = array ( 'title' => t('Blogs Additional Tags'), 'description' => t('Blogs Additional Tags'), );
$elements['blogs_short_desc'] = array ( 'title' => t('Blogs Short Description'), 'description' => t('Blogs Short Description'), );
return $elements;
}
After some research i did manage to find my way out:
I implemented hook_views_rss_namespaces to add a custom namespace and the custom items under the namespace:
function Mymodule_views_rss_namespaces() {
$namespaces['blogs'] = array(
'prefix' => 'xmlns',
'uri' => 'http://base.google.com/ns/1.0',
);
return $namespaces;
}
Note: Use the same 'prefix' and 'uri' as given in the above code or else namespace uri error will arise.

How to show fields below checkbox based on "checked" in Redux Framework?

I've downloaded the Redux Framework (using generator). I want some fields to be displayed if a checkbox field is checked (more related fields below checkbox).
I searched online and found some Redux code having checkbox_hide_below field which is not present in the current Redux Framework.
Will I have to do it all by myself or Redux framework provides some option to show/hide fields?
Lead dev of Redux here.
You'd have to do it yourself, however it's really pretty easy.
First, you'd do a get_option to your opt_name before you run your config. Then on the fields you want to make hidden you'd do something like this:
$options = get_options('OPT_NAME');
$field = array(
'id' => 'textarea_id',
'type' => 'textarea',
'hidden' => ( $options['test_value'] == 1 ) ? true : false,
'title' => __( 'Test Hidden Field', 'redux-framework-demo' ),
'desc' => __( 'This field will be set to be hidden if text_value is 1.', 'redux-framework-demo' ),
);
It's pretty strait forward. I hope that helps! You can also apply the hidden argument to sections as well. ;)

Overriding the user registration form in Drupal 6

I want to be able to customise the user registration form in Drupal 6
I have found thousands of tutorials that show me how to override the structure in which you can output the form as a whole, but I want to move form elements around etc and I cant quite seem to see the best way to do this
To expand on Jeremy's answer, you're going to want to study Drupal's Form API and user_register(). In short, you build an associated array; each element in the array corresponds to one form element.
Each form element in the array is its own associated array. They can have a type: textfield, select menu, checkboxes, etc.: see the Form API reference for all the types.
Each form element can also have a weight: this is how you order elements around. Lower numbered weights show up before higher numbered weights in the form.
One of the element types available to you is fieldset: this is what will allow you to group elements together. When you use a fieldset, it creates a section of the form with its own weight values.
So, let's say you have a form with three fields: Name, Company, and E-mail address. The Name should show up first, Company second, E-mail address third. You could specify the form like so:
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note the #weight key. If you wanted Company to appear after E-mail address, you'd set $form['company']['#weight'] to something higher than 3.
Now let's say you wanted to group Name and Company into a fieldset called Personal Information. Your form would now look something like this:
$form['personal'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
'#weight' => 1,
);
$form['personal']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['personal']['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);
Note that Name and Company are now array elements of $form['personal'].
If you want to make Name show up after Company in the fieldset, set its #weight higher than 2. Because the Name is now part of a fieldset that has a lower #weight than the E-mail address field, even if you set $form['personal']['name']['#weight'] to 4, it wouldn't make the Name show up after E-mail address.
So what you're going to attempt to do is use hook_form_alter() to alter the user_register form to change the weights of certain form elements, create your own fieldsets, and move certain form elements into your newly created fieldsets.
There are ways to do this within your theme, but I prefer creating a custom module for this. Create your custom module, and implement hook_form_alter():
function test_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'user_register') { // Only modify the user registration form
// Before you can get down to business, you need to figure out the
// structure of the user registration form. Use var_dump or kpr to dump
// the $form array.
// Note: if you want to use kpr on the user registration form, give
// anonymous permission to see devel information.
// kpr($form);
// Move Name field to after E-Mail field
$form['name']['#weight'] = 2;
$form['mail']['#weight'] = 1;
// Group Name and E-mail together into a fieldset
$form['personal_info'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
);
$form['personal_info']['name'] = $form['name'];
$form['personal_info']['mail'] = $form['mail'];
// The last block only copied the elements: unset the old ones.
unset($form['name']);
unset($form['mail']);
}
}
In more complex forms, moving things from one fieldset to another might yield unexpected results when submitting the form. This is because $form['name'] isn't the same as $form['group']['name'], which isn't the same as $form['other_group']['name']. You don't have to worry about that on the user_register form for the most part, but check out the handbook page on #tree and #parents for more information about this.
This covers modifying existing fields in the user registration form: if you want to add new fields, I highly recommend using Content Profile. If you want to create custom fields on your own, it's going to get a lot more complex as you're going to have to implement your own validate and submit handlers. Content Profile handles this for you: check out its README to see how to activate it for registration forms.
Using hook_form_alter you can do whatever you want with a form.
For example changing the weight can change the position on the page.
If you try:
MYMODULE_form_user_profile_form_alter(&$form, $form_state) {
// do your processing here
var_dump($form);
}
replacing MYMODULE with the name of your module.
You will see the structure of the form, you can change values in there to alter, labels weights descriptions etc.
In a module, first use hook_theme() , now assuming the name of your module is 'd6_forms' :
function d6_forms_theme() {
return array(
'user_register' => array(
'template' => 'templates/user-register-form',
'arguments' => array('form' => NULL),
),
);
}
This will make the user_register form look for a template, in the specified folder.
So make sure that in your module folder, there is a folder called 'templates', with a file 'user-register-form.tpl.php'.
You notice that in the hook_theme() , the extenstion of the template file ( .tpl.php ) is not supplied. That's normal, you don't need to specify it there.
Do make sure however, that the template has that extension, and that it's not just named 'user-register-form.php' !
In that template file, you have access to the $form variable , so print it there to see what fields are in there.
The devel module is recommened, since it's able to print big Drupal arrays in a fancy way ( using dpm() ).
If you do not have Devel module, or don't want to use it, this also works : <?php print '<pre>' . print_r($form, 1) . '</pre>'; ?>.
To print a field, just use <?php print drupal_render($form[field_name]); ?>, this will print the field and make sure that it works as intended.
So for example, if you want to print the 'name' field in the $form array, just use <?php print drupal_render($form['name']); ?>.
You don't have to print every field ! Just print the fields that you want to move somewhere ( which, with a basic Drupal register form, are about 3 : name, email & submit ).
To print all the remaining fields, just end your template with <?php print drupal_render($form); ?>.
It is important that you don't forget this, since the $form var contains stuff that is absolutely needed for your form to work ( like a token, etc .. ).
So good standard behaviour when templating a form, is to print that piece of code first at the bottom of your template.
This is an entire example of a small register form template, with some basic html :
<?php
// What is in that $form var ? To check, uncomment next line
// print '<pre>' . print_r($form, 1) . '</pre>';
?>
<div style="background-color:#ddd;padding:10px;">
<?php print drupal_render($form['name']); ?>
<?php print drupal_render($form['mail']); ?>
</div>
<div>
<?php print drupal_render($form['submit']); ?>
</div>
<?php print drupal_render($form); ?>
maybe this will help:
http://drupal.org/node/44910
You just need to Enable the Profile Module which would give access to place more fields in sign up form.
Go through this Simple Video tutorial which would be very helpful for beginners in Drupal .
http://planetghost.com/add_more_fields_to_sign_up

Resources