what hook to hook into for creating custom post types? - wordpress

I hate putting things into the functions.php file and therefore wanted to hook into a pre-existing WP-hook to register/configure my custom post-types (and taxonomies). Somewhere I was reading that "template-redirect" would be a good one but it appear that that hook isn't fired when you're on the admin pages and therefore is fairly useless.
Any help would be appreciated.

You can use init hook.
An example of registering a post type called "book".
function codex_custom_init() {
$args = array( 'public' => true, 'label' => 'Books' );
register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );
Reference: Codex.

I was reviewing my open questions and it reminded me that I hadn't closed this one out. Marty's answer was helpful but really pointed to a different solution path. In retrospect, I'm not sure what hook I had tried but the kind of obvious one is "init" and I'm using that now and it works.
Here's my flow:
My plugin is loaded with the 'plugins_loaded' event
It does some basic initialisation and then hooks into the 'admin' hook
When the 'admin' event is fired my plugin then this fires off the following function:
function add_hooks () {
// fire a hook that a configuration file can pick up
do_action ( 'lg_custom_types_definition');
// now fire hooks to register custom types
do_action ( 'lg_custom_type_cpt_registration' ); // register
do_action ( 'lg_custom_types_registered_post_types');
do_action ( 'lg_custom_type_tax_registration' ); // register
do_action ( 'lg_custom_types_registered_taxonomies');
}
This approach gives me a completely decoupled approach which means I can enable the "custom_types" plugin and I now have the 'capability' installed. Then I create a configuration plugin that hooks into the events that the capability has added.
Hope this helps.

You could use an include file within functions.php to include all your custom work.
<?php
// functions.php
include('inc/custom-functions.php');
?>
I've created a very simple page to create your custom post types,
you input the options you want for the custom field and it spits out the code needed in order to generate it in wordpress..
its located here: http://martin-gardner.co.uk/wordpress-custom-post-type-generator/
for example:
Input the Post Type Name: Video select where in the menu you want it positioned.
then select the options you want for that custom post type.
Edit labels, if you want, by default just uses name,
Read more on custom post types and the register_post_type #
http://codex.wordpress.org/Function_Reference/register_post_type
hope that helps a little ;)
Marty

Related

Wordpress REST api, how to seach custom field values

I'm using the Wordpress REST api to get all my post data, and using a frontend framework to display them. I've created several post types, who each have different custom fields (I created the fields using the Advanced Custom Fields plugin and the acf-to-rest-api plugin to add the fields to the json output).
Now I'm trying to use Wordpress' search function to search through custom field values, using a url like this:
/wp-json/wp/v2/articles?search=test
Currently, only the body text and title values are being used in the search, not the custom fields.
I've found a plugin to allow the search to include custom field values ( https://wordpress.org/plugins/custom-fields-search/ ), but when I installed it, nothing changed. I don't think the plugin works with the rest API.
Is there another way to achieve this?
Really late reply, but for people stumbling upon this post I have an answer to this problem:
add_action(
'rest_api_init',
function () {
register_rest_field(
'search-result',
'excerpt',
array(
'get_callback' => function ( $post_arr ) {
return $post_arr['excerpt'];
},
)
);
}
);
In the callback you can pretty much return anything you want, does not have to be specifically taken from the $post_arrvariable.

Restoring 'post' post-type via a child theme

I am working with a client who has chosen a theme which looks nice but actual removes a lot of WordPress functionality. Whether or not it is deliberate, it has removed the post type of 'post'. I found the hook they used to do this but unfortunately, they added it via a closure not a callback.
Below is a sample piece from the theme:
add_action('admin_menu', function () {
remove_menu_page("edit.php");
remove_menu_page("edit-comments.php");
}
});
The scenario is that I am creating a child theme to add back the WP functionality. The only way I can think to restore this is by adding another action that uses add_menu_page. I just don't really know how to restore it.
I may have to switch themes but they really like this one aesthetically. Guess the downside is that it reinvents the wp-admin backend. It wants us to add data through it's interface and not through the traditional 'post' and 'page' post types.
Anybody have any solutions?
I see only one solution - absolutely remove hook admin_menu and after do full restore (without closure). Of course, need more read and learn about hook admin_menu before operating. You can start from remove_all_actions
Solution No.2:
If closure callback is one in parent theme you can to use simple 'closure' remover :)
//remove closure callback
$hooks = $wp_filter['admin_menu'][10];
foreach ($hooks as $key => $value) {
if (preg_match('|^\d|', $key))
//closure's always started from 00000....(??)
remove_action('admin_menu', $key);
}

How to make your own post format in Wordpress?

How can I create my own custom post formats?
Or how can make my custom post type make work with a function like
get_post_format();
For example i have a custom-post type with the type of "accordion" and i like to be able to use it with as content element in the loop, but only if it exists...
get_template_part( 'content', get_post_format() );
So i am looking for a function like
get_custom_post_format();
which does not exists in Wordpress.
Anybody tried something similar?
I'm not sure if you're asking how to create custom post formats or custom post types so I've provided the answer to both.
If you're asking whether you can create custom post formats...
...then the answer is no. See the quote below from Post Formats on the WordPress codex:
The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature. Themes are not required to support every format on the list. New formats cannot be introduced by themes or even plugins. The standardization of this list provides both compatibility between numerous themes and an avenue for external blogging tools to access this feature in a consistent fashion.
If you're asking how to create a custom post type:
The most basic example of creating (registering) your own custom post type is to add the following code to your functions.php file inside your theme.
function register_recipes_post_type() {
$args = array( 'public' => true, 'label' => 'Recipes' );
register_post_type( 'recipe', $args );
}
add_action( 'init', 'register_recipes_post_type' );
The above code hooks our register_recipes_post_type function to be executed when the init action is triggered by WordPress core.
Once you've added this code, if you go to your wp-admin you'll see a new menu on the left called 'Recipes', that's your new custom post type. If you add a new recipe, give it a title and some content, publish it and then try to preview it, you'll notice that you get a 404 error. After creating a new custom post type you need to go to your Settings > Permalinks in your wp-admin, just visiting that page will fix your permalinks to include the new custom post type so if you now go back and refresh the preview of the recipe you just created you'll see that it now works rather than 404s.
Now if you create a new file called single-recipe.php and put some code inside it, just put 'test' now for the purpose of seeing that it works and once you have, refresh the preview of the recipe you just created once again and you should see that it just displays the word 'test'. Using that file you can create a completely custom template to be displayed for showing single entries (posts) of that custom post type, or you could use content-recipe.php if your single.php includes a get_template_part( 'content', get_post_format() ); as you said in your original post.
Obviously your custom post type probably won't be for recipes but just update instances of recipe and recipes to whatever you want it to be.
There are also other post type specific templates you can create too for your archive of the post type etc. The above should be enough to get you started though.
There are also other arguments you can pass when registering your post type, you can see the full list here: http://codex.wordpress.org/Function_Reference/register_post_type
I hope this helps. Good luck! =)
Creating New post format is not allowed currently by WordPress. you can’t define any post format apart from what WordPress allows.
Reference:
1. http://wp.tutsplus.com/tutorials/proof-using-post-formats/

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

In WordPress how do you register built-in taxonomies with custom post types in code?

The WordPress codex has a lot of examples of how to register custom taxonomies with custom post types, but I couldn't find much about using built-in taxonomies (tags & categories) with cpts.
I have a cpt called listings, and I need to add the standard category and tag UI elements to the listing cpt page. I also need to do this with code in my functions.php, rather than using a plugin.
Not a problem at all. When you register the post type, just add this argument to the array:
'taxonomies' => array( 'category', 'post_tag' )
Suppose you defined your cpt (custom post type) by the following:
register_post_type('listings', $args); // where $args is an array of your cpt settings
Then you could use the following to add taxonomy:
// category-like:
register_taxonomy('listing_category', array('listings'), array('hierarchical' => true, ...));
// tag-like:
register_taxonomy('listing_tag', array('listings'), array('hierarchical' => false, ...);
In fact, I personally put those custom type definitions in my own plugin (not open for public as it provide my own site functionalities, which obviously not suit the others at all).
The problem of putting in functions.php increases the difficulty to change to a new theme (although changing theme is not so often, but for self-owned blog, it do happen in some day).
Moreover, the custom post types should be site-wide, not depending on the current theme. So semantically it should not be in the theme's directory.

Resources