I'm new with Wordpress and I was asked to make some changes to a website. I need to change the following page's route (if possible):
/travels/poi
To:
/location/region/poi
Where travels is a listing of POIs (Points Of Interest). Is something like this possible?
EDIT: location/region/ is a page that lists my POIs
If travel is a custom post type, then you can define a custom permalink for it.
Looking for the code where you register the travel custom post type, something like this:
register_post_type( 'travels', $args );
Alter the rewrite argument:
$args = array(
// your ohter args
'rewrite' => array(
'slug' => 'location/region',
'with_front' => true,
'pages' => true,
'feeds' => true,
),
// some other args
);
After you made this changes, you have to flush the rewrite rules or the changes takes no effect. To do this, just go to Settings -> Permalinks in wp admin (you don't have to change anything).
Related
I have a WP multisite set up with sub-domains for all sites.
I would like the URL structure for each new site to be:
subdomain.maindomain.com/post-name
For instance: subdomain.maindomain.com/hi-world
I've been struggling with this for a day and a half, and I hope somebody here can help me. I have read a LOT of info, and tried with editing the default theme's functions.php and also adding a custom plugin in mu-plugins. But I have not succeeded yet.
So the regular setting for permalinks is now: /%year%/%monthnum%/%day%/%postname%/
But I'd like to have: /%postname%/
Ok, I have tried many different things. This is probably the closest to success (?):
// set permalink
function set_permalink(){
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%postname%/');
}
add_action('init', 'set_permalink');
In the functions.php I have this for adding standard pages to the new blogs/ sites automatically:
add_action('wpmu_new_blog', 'wpb_create_my_pages', 10, 2);
// create new page
$page_id = wp_insert_post(array(
'post_title' => 'My title',
'post_name' => 'The name',
'post_content' => 'The content here',
'post_status' => 'publish',
'post_author' => $user_id, // or "1" (super-admin?)
'post_type' => 'page',
'menu_order' => 10,
'comment_status' => 'closed',
'ping_status' => 'closed',
));
So in this I have tried the 'set_permalink' function, but it doesn't work.
I have also tried making my own mu-plugins plugin, but haven't got that working neither.
When I Google I keep finding solutions that require the new blog's owner to log in and save the permalink structure, but I simply want the permalink structure to be the same for all new blogs/ sites.
How can I set the default permalink structure for new sites?
Thanks for any pointers or code that can help me with this!
Just for future proofing this question with an answer, and for reference for others:
I created a new sub directory in /wp-content/ named "mu-plugins", so I had /wp-content/mu-plugins/
In "mu-plugins" I created a file named "rewritepermalinks.php"
In it I put:
<?php
/**
* Plugin Name: Permalin Structure Edit
* Description: Edit the permalinks structure on new multisite sites
* Author:
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_action( 'wpmu_new_blog', function( $blog_id ){
switch_to_blog( $blog_id );
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%postname%/');
$wp_rewrite->flush_rules();
restore_current_blog();
}, 10 );
?>
This then worked for me on Wordpress 5.8.3 and with multisite enabled. The solution was found here and also mentioned in the comments for the question. I had tried it once before but must have had a typo because it didn't work the first time.
I have tried to use 'wp_insert_site' in stead, but haven't got that to work. There is a hint in the comments here about wp_insert_site that might be of value to get this working.
For now the above code works for me as a MU plugin.
I have created a custom post type wrestling and created its corresponding custom fields using Advanced Custom Fields. Now, I wanted the users to fill this custom form on the front end, so that on submission, the data would get automatically updated in the custom post type in the dashboard. For this purpose, I created a custom page and assigned a custom template to it which contained the required form. There are four HTML form fields that the users are supposed to fill, named name, venue, main_event and fee respectively.
The custom form fields that I created using Advanced Custom Fields are named as promotion_name, venue, main_event_ and price respectively. Now, in order to fill the data entered by the users on the front end onto the custom post type fields at the dashboard, I tried using the wp_insert_post() function as follows:
$post_information = array(
'promotion_name' => $_POST['name'],
'venue' => $_POST['venue'],
'main_event_' => $_POST['main_event'],
'price' => $_POST['fee'],
'post_type' => 'wrestling',
);
wp_insert_post( $post_information );
However, after the user submits the form, a new entry (no_title) does appear in my custom post type, but the custom form fields are still empty (See images below:)
I'm sure this is because I'm not using the wp_insert_post() correctly for updating custom post types. I'd really appreciate some help here. Thanks.
PS: This is how I have defined my custom post type in functions.php:
<?php
function wrestling_show_type()
{
register_post_type('wrestling',
array('labels' => array('name' => 'Wrestling Shows', 'singular_name' => 'Wrestling Show'),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'wrestling')));
flush_rewrite_rules();
}
add_action('init', 'wrestling_show_type');
?>
If you have used ACF, you should use their API to interact with the fields. There's a method called update_field() that does exactly what you are looking for. This method takes 3 parameters:
update_field($field_key, $value, $post_id)
$field_key is an ID ACF gives to each field you create. This image, taken from their very own documentation, shows you how to get it:
Edit: $field_key Will also accept the field name.
$value and $post_id are pretty straight forward, they represent the value you want to set the field with, and the post you are updating.
In your case, you should do something to retrieve this $post_id. Fortunately, that's what wp_insert_post() returns. So, you can do something like this:
$post_information = array(
//'promotion_name' => $_POST['name'],
'post_type' => 'wrestling'
);
$postID = wp_insert_post( $post_information ); //here's the catch
With the ID, then things are easy, just call update_field() for each field you want to update.
update_field('whatever_field_key_for_venue_field', $_POST['venue'], $postID);
update_field('whatever_field_key_for_main_event_field', $_POST['main_event'], $postID);
update_field('whatever_field_key_for_fee_field', $_POST['fee'], $postID);
So basically what you're doing is creating the post first, and then updating it with the values.
I've done this kind of stuff in the functions.php file, and it worked fine. From what I've seen, I think you are using this routine in a template file of some sort. I think it's gonna work fine, you just gotta make sure the ACF plugin is activated.
EDIT:
I forgot the promotion_name field. I commented the line inside $post_information, as it's not going to work. You should use update_field() instead, just like the other 3.
update_field('whatever_field_key_for_promotion_name_field', $_POST['name'], $postID);
There is a way to add custom fields data directly when creating a post. It is mentioned in the wp_insert_post() docs. Just pass an array with custom field keys and values to meta_input like this:
wp_insert_post([
'post_status' => 'publish',
'post_type' => 'wrestling',
'meta_input' => [
'my_custom_field_1' => 'beep',
'my_custom_field_2' => 'boop',
]
]);
I'm looking into this approach to avoid a dozen of update_field() database calls and make it less resource intensive, but I have yet to prove this approach actually makes less database calls in the background.
Im a TOTAL newbie to drupal development so please help me here, ok i have created a custom module which so far creates a custom database how do i go about creating a list page in the backend that i can use to manage each item in the DB and how do i go about creating a custom edit form to manage the insert/ edit / delete of each item
function rollover_res_schema() {
$rollover_res = array();
$rollover_res['rollover_res'] = array(
// Example (partial) specification for table "node".
'description' => 'Positioning for rollovers',
'fields' => array(
'rollover_res_id' => array(
'description' => 'The primary identifier for a node.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'rollover_res_actual' => array(
'description' => 'The main rollover plain text.',
'type' => 'text',
'length' => 255,
'not null' => TRUE,
),
),
'indexes' => array(
'rollover_res_id' => array('rollover_res_id'),
),
'primary key' => array('rollover_res_id'),
);
return $rollover_res;
}
If you're a total newbie to Drupal development you should not be writing ANY code for the first month or two and you shouldn't do custom database code the first 6 months.
Start with learning about Fields and Views and once you grasp these you can add one of Display Suite, Context or Panels.
The key to learning how to do things in drupal is:
1) google search how
2) see how other modules do it. In this case, look at some core modules, such as the block module. In there you'll see the schema in .install, and you'll see some functions that create forms for saving new blocks, such as block_add_block_form. You'll need to read up on the form API. But basically, you'll create a form hook to display a form, a menu hook to create a page to hold the form, and a hook to submit the form. If you grep through your code base, you'll see many of examples that you can copy. In fact, there are drupal example modules you can download that cover most of the basics: https://www.drupal.org/project/examples
But to learn how to interact with the database, you could find a module that does something similar to what you're doing and look at how it uses hook_menu to set up page callbacks, forms for editing data.
I am creating my own posts in wordpress however I have hit a little problem and im not sure how to fix it.
The below register_post_type creates an Invalid post type error.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'talent',
array(
'labels' => array(
'name' => __( 'Talent' ),
'singular_name' => __( 'talent' )
),
'public' => true,
'has_archive' => true,
)
);
}
it doesn't seem to like the word talent. If I change 'talent' to 'arist' it works. However it needs to be 'talent' for the URL. Ive checked on wordpress and using talent shouldn't cause any conflicts with default wordpress settings.
That's indeed quite odd, I tried your snippet which is working as expected. That makes me think probably you are trying already to register the same post type before through another method. It could be either via plugin, or in the same function.php file, or using a plugin to create custom types on the database (like CPT UI for example).
I suggest you both of the following:
Track down the real nature of the problem, in case it's not messing up something else for you. You probably might encounter another similar problem in the future which will surely waste your precious time.
Avoid general namespaces especially with global shared stuff like the custom post types, I'll generally call them projectname_postname so, for example, you'll end with something like this: stackoverflow_talent
Assumed you go for the second one, just use the slug rewrite for that particular post type in this way:
register_post_type( 'myproject_talent',
array(
'labels' => array(
'name' => __( 'Talent' ),
'singular_name' => __( 'talent' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'talent'),
)
);
Now all your posts will show the /talent/ path.
Remember to flush the permalink structure after you create the post type, otherwise you cannot see that on the frontend.
Hope solves your problem.
I had the same issue but the answer did not work for me. In case anyone else finds this, make sure the post type name is not longer than 20 characters. That might be the issue and Wordpress will not care to let you know.
Credits go to: https://stackoverflow.com/a/26029803/722036
I have url http://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms
Where "real-estate-news" is category and "phuket-luxury-property-in-high-demand-ms" is the post name .
when I print $wp_query->query_vars['name']; it gives the post slug and $wp_query->query_vars['category_name'] gives the category slug.
I want to generate a new url like http://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms/xyz
and want to get xyz in query var like
echo $wp_query->query_vars['name']; // print "phuket-luxury-property-in-high-demand-ms"
echo $wp_query->query_vars['category_name']; // print "real-estate-news"
echo $wp_query->query_vars['section']; //print "xyz"
How to add section query var in wordpress please help me
Is this a Custom Post Type? If it is, you have more options than not. Within the register_post_type()'s array you can enter an argument for 'rewrite' to change the slug name and hack in specific rewrite rules for that particular CPT. I have put this project away because of its complexity and if you find an answer I'd love to hear it. Here is my notes on the matter.
register_post_type(array(
//options
'rewrite' => array(
'slug' => 'beach', //a slug used to identify the post type in URLs.
'with_front' => false,
'feed' => true,
'pages' => true
),
dreamdare.org
shibashake.com1
shibashake.com2
Beware of wp.tutsplus very often misinformation is offered there by authors themselves.
wp.tutsplus.com
The way I managed to do this is adding a rewrite rule.
This will look very similar to the one that custom post type creates but also receives a third parameter.
so in your case the rewrite rule would look like this
add_rewrite_rule(
// post-type | post-slug | section
'^real-state-news/([^/]+)(?:/([0-9]+))?/([^/]+)/?$',
// | Here the section is added
'index.php?post_type=real-state-news&name=$matches[1]§ion=$matches[3]',
'top'
);
//You then need to add a tag to it
add_rewrite_tag('%section%','([^&]+)');
// At this point, you should be able to grab that param
get_query_var('section')