I want to know something about this article
In order to create custom post types, open your template’s
functions.php file in an editor, and
place the following function within
the file:
Where is this file function.php in wordpress 3.0.3
You must open the file functions.php that is in your template directory, in /wp-content/themes/your-theme-name.
If the file is not present, create it.
It is /wp-content/themes/[name-of-theme]/functions.php
e.g if twentyten (default theme)
/wp-content/themes/twentyten/functions.php
Custom post types and custom taxonomies should be added through a plugin, and not through a theme. It is the recommended way to add these.
Just a couple of reasons why you should add custom post types and taxonomies via a plugin
custom post types and custom taxonomies add functionality to your site, and not your theme. They also do not add visual benefit to your theme
custom post types and custom taxonomies should remain available across themes. If a theme is changed, these should still be available to the next theme activated
Related
We have develop a custom plugin for Woocommerce payment integration and we need to add woocommerce hooks, in our plugin file without including it in Theme folder's function.php.
How can we Add custom hooks to our plugin that we can invoke from function.php file? is there any workaround for it?
(when we add custom woocommerce hooks and action code in function.php file in wordpress theme then code work fine but we need it in our custom plugin file.)
Any help will be highly appreciated.
There is 2 kind of hooks: Action hooks and filter hooks.
An action hook is like a gate or a door in some code, that allow you to run some custom code, in a specific code location. It will be executed or "triggered", when the code that handle that door or gate runs. So it's event based.
Filter hooks are a bit different than action hooks. They are not use to trigger some custom code (not event based). They allow to alter or manipulate some existing code values, as strings, arrays, objects… So filter hooks have always at minima one argument to be manipulated and will always return that manipulated argument.
Create custom hooks in a plugin:
1) For action hooks: do_action() Wordpress function
do_action('woocommerce_my_custom_action', $some_variable, $another_variable );
2) For filter hooks: apply_filters() Wordpress function (where $value is the manipulated argument)
$value = apply_filters('woocommerce_my_custom_action', $value, $some_variable, $another_variable );
There is a bunch of documentation, tutorials and ressources on internet regarding hooks
Wordpress filter Vs. action
what is difference between action hook and filter hook in wordpress?
Wordpress Coding a custom action hook
Your plugin should have its own functions in its own php files, it almost sounds like you are pasting the code into the theme's function.php file? And its being overwritten when the theme is updated? Is this what you are doing?
Please read the wordpress plugin development docs.
https://developer.wordpress.org/plugins/intro/
If your plugin requires other plugins, there are several ways of making the user activate the other plugin(s) first:
https://wordpress.stackexchange.com/questions/127818/how-to-make-a-plugin-require-another-plugin
I am using Woocommerce Projects plugin for WooCommerce and I have made some extra meta fields for the Projects.
But how can I display them on the place where I want them?
On this example page they show an option that will show it under the short description. But how can I change the location, to put it everywhere I want in the template file.
Update (see below: Overriding Projects template files safely)
What you are looking at, is this documentation, that lists all possible hooks with WooCommerce "Projects" plugin.
So you will have just change the hook in:
add_action( 'projects_after_loop_item', 'display_new_projects_fields', 10 );
Replacing 'projects_after_loop_item' by one of the hooks below to change the displayed location around the loop (for example):
projects_before_loop
projects_before_loop_item
projects_loop_item
projects_after_loop_item
projects_after_loop
Or also (with the other "Projects" plugin templates):
## General layout
projects_before_main_content
projects_after_main_content
projects_sidebar
## Archives
projects_archive_description
## Single Projects
projects_before_single_project
projects_before_single_project_summary
projects_single_project_summary
projects_after_single_project_summary
projects_after_single_project
Overriding Projects template files safely
Copy the templates directory located in projects-by-woothemes plugin folder to your active child theme directory (or active theme directory) and rename it projects.
Projects plugin will always check your theme for these files before displaying core templates as a fallback. Any template files found in the theme will be given priority and the core counterpart ignored.
Now you can edit any template file as you want, adding inside it this little peace of code (from the example), directly just where you want, displaying your custom field value, just using:
echo esc_attr( get_post_meta( $post->ID, '_location', true ) );
At the end, is better to keep in that folder, only the customized templates.
Reference and related:
Editing Projects Templates Safely
Overriding Templates via a Theme
I have a WordPress theme with a few post types, the theme runs on redux framework.
I want to add to a specific post type a custom field from the functions.php file in child theme without editing core files/installing plugins for it.
How could i do it? looking for something like:
if(is_post_type("some_post_type")&& is_admin_single_editor()){
// Add custom field to edit screen
}
Thanks
I have a wordpress theme applied to my wordpress site.
It has its own custom post type named 'company'. I am trying to enable archiving on this post type but I cannot find where I can do that.
Resources online suggest it would be in functions.php of the theme but it does not seem to be there.
Is there anywhere else in Wordpress where an author can define their own custom post types?
Note: It is also not visible within the Types plugin.
In many WordPress plugins, there are instructions that Place <?php do_action('plugin_name_hook'); ?> in your templates.
where exactly this code needs to be placed and what should be the plugin_name_hook?
hooks are actions performed at a time.
If you want a custom hook then
1.create your custom hook in your plugin or theme's function.php
add_action('my_action','my_function');
function my_function()
{
// do something
}
2.and call it in your template as
do_action('my_action');
and you can read more here
https://codex.wordpress.org/Function_Reference/do_action
I don't really like plugins that you still need to modify your theme's template file manually. What is the use then of a plugin.
To come back to you question though, your do_action() call will need to go in the template where you need to display the output of the plugin. Say it is a plugin that add social share buttons to the content, you will need to add the do_action after the_content() in your template files.
The second part of the question, the plugin_name_hook will be specified by the specific plugin. So far that you have to follow the installation instructions of the plugin