How to create a permalink programatically?

Permalink Manager provides a few useful API functions that could be used to create permalinks programatically. If you do not want to deal with the raw PHP code, you may use the plugin to update the permalinks individually or in bulk (with Permastructures editor) directly from admin dashboard.

How Permalink Manager stores custom permalinks?
If you are curious how the custom permalinks are saved visit a separate article for additional information.

How to generate the default URI?

Please skip this step, if you would like to set the custom permalink with your own code.

If you just want Permalink Manager to store the custom permalink based on your default permalink formats, you must first generate it. Depending on the content type, you will need to use one of two following functions to generate the custom permalink based on your current permastructures settings.

As you can see below, only one argument is needed ($post or $term). You can use either the full object (instance of WP_Post() or WP_Term() class) or just its ID (as integer).

// A. [Posts, Pages, CPT items]
$new_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post);
// B. [Categories, Tags, custom taxonomy terms]
$new_uri = Permalink_Manager_URI_Functions_Tax::get_default_term_uri($term);

Now, when the variable with new URI is set you will need to save it in the custom permalinks array. To do so you will need to use Permalink_Manager_URI_Functions::save_single_uri() function.

Permalink_Manager_URI_Functions::save_single_uri($post_or_term_id, $new_uri, $is_term, $save_in_database);

The function requires four arguments:

Variable Description
$post_or_term_id The ID of the post/term whose permalink you want to change (integer)
$new_uri A new custom permalink that you want to use (string)
$is_term Set “true” if you want to update post permalinks, and “false” if you want to update terms (boolean)
$save_in_database If you wish to change the permalink in the database, set “true”. (boolean)
/**
* A. [Save Posts, Pages, CPT items permalinks]
*/
$post = ...
$post_id = ...
// A1. Generate the URI (optional)
$new_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post);
// A2. Save the URI in DB
Permalink_Manager_URI_Functions::save_single_uri($post_id, $new_uri, false, true);
/**
* B. [Save Categories, Tags, custom taxonomy terms permalinks]
*/
$term = ...
$term_id = ...
// B1. Generate the URI (optional)
$new_uri = Permalink_Manager_URI_Functions_Tax::get_default_term_uri($term);
// B2. Save the URI in DB
Permalink_Manager_URI_Functions::save_single_uri($term_id, $new_uri, true, true);

How to get current URI programatically?

The following two functions may be used to read the URI address assigned to a single post or term. If the custom permalink for this specific item was not previously stored in the custom permalinks array, Permalink Manager will provide the default URL as a fallback.

Create a permalink using URI Editor
To programatically obtain the value of “Current URI” field use the functions listed below.

How to get permalink of specific post?

As with the previous function that returns the default URI, just one parameter ($post or $term) is required in this case. Both functions will accept either a full object (instance of the WP Post() or WP Term() classes) or its ID as a parameter (integer).

The above will just provide the URI, however you may obtain the complete URL address by using the WordPress inbuilt function get_permalink().

Permalink_Manager_URI_Functions_Post::get_post_uri($post)
Variable Description
$post The post ID or object (integer/WP_Post object)
Permalink_Manager_URI_Functions_Tax::get_term_uri($term)
Variable Description
$term The term ID or object (integer/WP_Term object)
Go up