wordpress my own custom field - php

I want to make custom fields in WordPress post.
I am a developer and want to create my own custom field. I don't want to use the default custom fields that are available when we install WordPress. In fact, I want to add radio
buttons and a check box from where I could select them as you can see in the image I attached.
How do I go about this?

Everything will be done in functions.php.
You have to declare your extra fields (meta boxes). This article will give you all the explanations : http://www.farinspace.com/how-to-create-custom-wordpress-meta-box/
It should give you all the info you need to add meta boxes (see also : http://codex.wordpress.org/Function_Reference/add_meta_box)

Related

WORDPRESS: storing meta (check) box values with register_setting (possible?) and posting a form to content section of pages

this is my first time making a wordpress plugin and I am a novice developer.
I need to create a checkbox that gives the admin the option of including a form within the content section of a page. The admin will be given the option of either enabling or disabling this form for each page based on whether the box is ticked or not. I have a couple of problems:
not sure if I can save metabox values to admin settings? if not can I create a settings field in the form of a metabox to store the setting? I want the option to appear at the bottom of the page editor
How can I then post the form to the content section of the page? I need an if statement to check if the box is ticked; is there a way I can then ''do_shortcode()'' to echo out the form into the editor and thus the content section of the page, not touching any other part of the page?
I'm just looking for ideas at this stage.
Thanks in advance
If you need one checkbox that adds a form to every page, make it a system setting - this is different than a metabox - it is something you will retrieve with get_option(), and save with update_option()
If you need a checkbox on every page's edit form to decide whether or not that particular page outputs the form when viewed, then you want to add it as a metabox to the page. This is something you'll get with get_post_meta()
The main difference is a settings value is post-independant. It has one value for everything. A meta value is post-dependant. It is stored in the postmeta table by the post's ID and, as such, each post can have a different value (or even multiple values - so be careful when coding this - you'll probably want to exclusively use update_post_meta(), rather than add_post_meta())
As for how to post the form to the page you'll either want to edit your theme's single-page.php file, check for the admin or meta value, and output the form. Or you could look into the filter 'single_template' to load a completely different template depending on the setting / meta value
So, in short:
1) No you cannot save a metabox value to an admin setting. You want a metabox for that particular content type using the add_meta_box() function
2) You will most likely need to edit your theme (preferably by creating a child-theme)

Hide the whole metabox cmb2

i'm using the famous CMB2 to create custom metaboxes, i want to have one hidden field with default value which i will set myself. i used this hidden field type and its working fine , the thing is the metabox will have this field only, so i ended up with what looks like an empty metabox which may confuse the user. i have read dispaly options but this controls where should the metabox appear, i have it exactly where i want on my posts but i want to set it's visability to be hidden so i do not confuse the user , i thought about using css on this but i want to check if there is a way to achieve this using CMB API
i'm adding the css way answer because it works and achieves the requirement , but i would still like to know if there is another php way for educational purpose.
function admin_style() {
wp_enqueue_style('admin-styles', get_template_directory_uri().'/css/admin-css.css');
}
add_action('admin_enqueue_scripts', 'admin_style');

Display custom field tabs in front end article edit page in Joomla 3.x

I have been looking for a solution to display custom field tabs in the Joomla
front article edit page.
http://www.aixeena.org/extensions/aixeena-easy-cck
Aixeena Easy CCK
I installed the plugin of aixeena, two custom field tabs: the Extra content and Extra content 2 are displayed in the backend admin article edit page, which is great. But we also need to let regular edit their article and enter values in custom fields under those two tabs. Any idea how to achieve this?
I tried a couple of other sites posting the solution of adding custom fields in article, such as Rating region described in the site:
https://docs.joomla.org/Adding_custom_fields_to_the_article_component
I was not able to make the Rating tab displayed in the front end article edit page neither.
I tried the fieldattach, it does supports custom field tab ( which is the group name of custom field), the list field type only contains static values, we have to define fieldset and field type through xml file.
Any help is greatly appreciated.
I just finished writing a couple pluginsto do this similar thing. I wrote my own plugin to add the extra fields - but it looks like the one you've already used is very similar.
I had a really hard time figuring out how to add the fields for frontend editing as well. But I finally figured it out.
First, you're going to need to copy the com_content form edit.php into a template override (/templates/your_template_name/html/com_content/form/edit.php).
In that file, add the extra fields you have. You'll need the name of the field that you put in the xml file for your plugin you already have. Add them right before/after the fields that are already in the file, that you want your fields to appear before/after. Example:
<!-- CUSTOM begin custom fields -->
<?php echo $this->form->renderField('video_url', 'attribs'); ?>
add all your fields in there - replacing 'video_url' with your own field name. The plugin you already have already saved these fields to the 'attribs' part of the article, so you can leave that there.
The plugin that I used to create the fields for the backend is a really simple one based off of this tutorial https://zunostudios.com/blog/development/203-how-to-add-custom-fields-to-articles-in-joomla
What I'd suggest is to go ahead and create that plugin too. Except in the zfields.php file, change "$app->isAdmin()" to "$app->isSite()" - make sure the xml files have the same names as your other plugin xml files, so they save to the same place, and you're done. The fields will show up on the frontend edit form now.
What I'd really recommend is to get rid of the Aixeena plugin, and just make that easy plugin using the tutorial above - it's super easy. When you make it, duplicate the "if ($app->isAdmin())" section, copy it right below it, and change "isAdmin" to "isSite" - and now you've got the entire thing, all your custom fields, both frontend and backend, all in one place. Now just do the edit.php template override, and you'll have them displayed in both the front and backend. I hope that helps - if you need I can upload more example code.

Add Custom Field Key in functions.php

Is there any way to add custom field keys to the dropdown of the custom fields on the "add new post" page without adding the fields?
Check add_meta_box (It allows plugin developers to add sections to the Write Post, Write Page, and Write Link editing pages. ):
http://codex.wordpress.org/Function_Reference/add_meta_box
Or these plugins:
Custom Field Template: http://wordpress.org/extend/plugins/custom-field-template/
Flutter: http://flutter.freshout.us/ (this one may be what you are looking for)
Magic Fields: http://wordpress.org/extend/plugins/magic-fields/
Source: http://sltaylor.co.uk/blog/control-your-own-wordpress-custom-fields/
Adding Meta Boxes is definitely the way to go. The following should help you:
http://farinspace.com/wordpress-meta-box-next-level/
http://farinspace.com/custom-fields-area-uncluttered/

Filtering custom fields on wordpress admin page

I'm currently working on a wordpress 2.9.2 plugin (my first) that creates additional custom fields that will later be read from the template. The plugin adds an additional meta box to the "Edit Post" screen which is responsible for manipulating these values.
My problem is that the custom fields generated by the plugin also show up in the "Custom Fields" box. Ideally I would like for the custom fields generated by the plugin to be managed separately from any other custom fields for that same post/page.
Is there a way that I can filter fields specific to my plugin to prevent them from being displayed in the "Custom Fields" box? I have yet to locate any action or filter that will let me capture and manipulate those values before they are rendered on "Edit Post".
Additionally, if you prefix your custom fields with and underscore "_" it will be hidden from the custom fields section.
Also, check out my WPalchemy Meta Box PHP Class which will help you create meta boxes with ease.
If you take a look inside the function _list_meta_row() (around line 2500 in wp-admin/includes/template.php), you'll see it skips post meta that is a serialized array or object.
So, if you can see fit to store your plugin post meta as an array or object, it shouldn't show in the Custom Fields box.

Categories