Add specific words to WordPress custom type slug - wordpress

I have a WordPress installation with a custom type (places) wich produces this permalink structure:
http://example.com/places/the-first-site/
And I would like to show all my sites like this:
http://example.com/places/visit-the-first-site-and-enjoy/
where 'visit' and 'and-enjoy' would be always those specific words (constants).
Even better I would like to put a custom taxonomy I have (year) as a metadata
places/visit-the-first-site-and-enjoy-1985/
I can access the DB and modify the post name of all post, but I would have to do for the new post also, and I'm looking for some automated rule, but can't find how to do.
Maybe some rewrite rule, but I don't know how to do it.
Any ideas??

You do not have to edit anything in your database nor any codes in your WordPress installation; you can achieve that directly from your WordPress administration panel.
From your Dashboard, click on the Permalink sub-menu under Settings; there, you will have to select the Custom Structure option and set it as follow in order to achieve your desired effect:
/places/visit-%postname%-and-enjoy-%year%/
Please note: here, we made use of both %postname% and %year% Structure Tags so as to get names of posts with their corresponding year of publication respectively.
Don't forget to click on the Save Changes button on the page in order to effect your changes.
... Read more on Permaklinks (for general knowledge).
For a custom post type and taxonomies as expressed further in comments, you will need a custom solution which will require a little bit of coding and or tweaking, depending on your abilities; you may use this handy plugin (Custom Post Type Permalinks) from the WordPress.org Plugins repository.
This posts should equally be of great help to you, to getting started and understanding further, should you chose to code.

You have différent hook like save_post or pending_to_publish, where you can set or change the "post_name" (that's corresponds to the permalink slug).

To Add specific words to WordPress custom type slug You have to Register custom rewrite rules.
suppose this is Your URL http://example.com/places/the-first-site !
and you have a post type places.
function add_rewrite_rules()
{
$wp_rewrite->add_rewrite_tag('%places%', '([^/]+)', 'places=');
$wp_rewrite->add_rewrite_tag('%state%', '([^/]+)', 'state=');
$wp_rewrite->add_permastruct('places', 'places/visit-%state%-and-enjoy/', false);
}
function permalinks($permalink, $post, $leavename)
{
$no_data = 'no-data';
$post_id = $post->ID;
if($post->post_type != 'story' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$state = get_post_meta($post_id, 'location', true);
if(!$state)
$state = $no_data;
$permalink = str_replace('%state%', $state, $permalink);
return $permalink;
}
add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);
put this code in your custom post type plugin. and if get page not found error please change your permalink setting. or make a custom template file to show this post.

Related

WP hook changes view in admin

I have a custom post-type called reference. When I save a standard wp-post I would also like to save a copy of that post with the only difference is that it has post_type = 'reference';
I am using the following hook:
add_action('publish_post', 'create_reference', 10, 3);
function create_reference($id)
{
$post = get_post($id);
$ref = $post;
$ref->post_type = 'reference';
wp_insert_post($ref);
return $post // Seems that I need this line, otherwise the standard post wont save.
}
This seems to work but also changes the view in wp-admin which is confusing. Even though i am creating the post in the standard post view. The view changes to the reference post-type view.
I do not understand why this is happening and how it can be avoided.
Thank you.

Custom Columns Don't Display Anything

I have added a new column to a custom post type to display the post's ID. This works for the WordPress core post type, but not for my custom post type. I have tried using the manage_{post_type}_custom_column hook and just applying it to all posts, but neither works.
It DOES add the custom column headers, but I can't populate them with anything at all when viewing the custom post type.
This is what it looks like when viewing the custom post type
and
this is what it looks like when viewing a regular core post.
// Add post ID column to use an order ID in all posts view.
add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);
function oms_order_id_header($columns) {
//Remove title column
//unset($columns['title']);
//Add new columns
$columns['order_id_header'] = 'Order ID';
$columns['customer_header'] = 'Customer';
$columns['author'] = 'Owner';
return $columns;
}
function oms_order_id_column( $column, $post_id ) {
if ($column == 'order_id_header') {
echo $post_id;
}
}
This turned out to be an issue with the post type being set to hierarchical. Hierarchical post types need to be targeted by a different action hook than the one used here.
Instead of manage_posts_custom_column, hierarchical post types need to use manage_pages_custom_column.
I just tried you code and it seems to be working perfect on my WordPress installation in both cases: Custom Post and Post.
Maybe your error is happening because your posts are drafts? I don't think so but maybe. (I tried your code also with drafts on my installation and it worked). Here is the screenshot:
Try printing "hello world" instead of the $post_id to check if it prints in all cases.

How to create two different single.php for same post type?

I have a post type named 'Property'.
I want to show single post in two different way.
if anyone click on post then it will shows a simple layout with name of post and description.
Now i have also category for beds. now if anyone goes in category '2 Bed' then you can see all post with '2 Bed' categories('its done'). but now if anybody click on post then it have to show different single page.
my English is very bad so please excuse it.
You can set up individual templates for a single category by using the single_template hook.
Put this in your functions.php file:
function my_category_templates($single_template) {
global $post;
if ( in_category( 'property' )) {
$single_template = dirname( __FILE__ ) . '/single-property.php';
}
// Copy the above for your other categories
return $single_template;
}
add_filter( "single_template", "my_category_templates" );
You can then create individual single templates for each category, just add more conditions and point them to the template you create.
There is the concept of Category Templates as dictated by the
Template Hierarchy but because you are asking how to display a "single" post based on category, you will want to use the in_category() Conditional Tag in the Template file you use to display singe posts. The Loop article has an example of using in_category.
Or look at this concept:
http://www.nathanrice.net/blog/wordpress-single-post-templates/
Or this:
http://justintadlock.com/archives/2008/12/06/creating-single-post-templates-in-wordpress
Or this plugin:
http://wordpress.org/extend/plugins/custom-post-template/
Or this plugin:
http://guff.szub.net/2005/07/21/post-templates-by-category/
So according to the Wordpress template hierarchy there is only one single.php, that cannot be separated by category (like the archive page for example.)
https://developer.wordpress.org/themes/basics/template-hierarchy/
So in this case I suggest you read the current category id in your single.php file and then adjust the content to your needs. You can use get_the_category() do do this (reference: https://developer.wordpress.org/reference/functions/get_the_category/) which will return you an array with categories. In my simple example I just pick the first category to do something:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 1) echo 'do something here';
Thanks for you help. I am done with it myself.
I create single.php for simple format and and in category result i didn't use the_permalink and call id of post and made a url like http://localhost/demo/page?id=$id

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

How do I add content types other than Posts in the WordPress admin area?

I want to add a completely custom content type to the WordPress admin panel as per my image below. I don't believe this is called a plugin as I did a tutorial on those and they don't have an admin interface. I want to define a custom create/edit/delete screen for this content.
Is this possible?
What should I be searching for to get help on this?
I believe what you're looking for (as of Wordpress 3.0) is custom post types. There's quite a good tutorial on them here, however googling "wordpress custom post types" should provide a plethora of links.
http://codex.wordpress.org/Function_Reference/add_submenu_page is a good starting place to look. It will explain how to add new items into the menu.
Now, removing them is a different story. This function I wrote for one of my plugins removes entries from a submenu:
function cleanup_menu() {
global $submenu, $wpdb;
$new_submenu = array();
$remove = array( 'Cast Manager', 'Seating Manager', 'Download Reports', 'new report' );
foreach ($submenu['menuname'] as &$item) {
if ( ! in_array( $item[0], $remove ) ) { $new_submenu[] = $item; }
}
$submenu['menuname'] = $new_submenu;
}
So in your case, "menuname" would be changed to "Posts", I would guess. There's also a function remove_submenu_page, but there's no documentation on it and I haven't looked into it.

Resources