How to update update existing custom post type with register_post_type permanently? - wordpress

I want to perform quick rewrite slug update for my custom post type on the fly. So I use code like in my theme's functions.php:
$no = get_post_type_object($pt_slug);
$no -> rewrite['with_front'] = false;
$no -> rewrite['slug'] = $slug;
register_post_type($pt_slug, $no );
Which is hooked to 'init' add_action('init', 'check_post_type_rewrite_url');
There is large code so I write only issue part in here.
register_post_type returns object with updated slug. I understand it means my custom post type was updated, but it was not. Existing post type still has its old rewrite slug. Should I add something special for rewrite rules to save changes and make it work? Or there is specific way to register/update post types with rewrite slug so it worked?

The way you solved the issue - by using flush_rewrite_rules - is technically correct but not recommended. As stated in the doc:
This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when absolutely necessary.
So you shouldn't keep your flush_rewrite_rules() call in your init hook, as it will regenerate all the rewrite rules at every page load, which is really badly ressource consuming.
Usually it's enough to just visit the Permalinks Settings page to flush the rules - so if you change again your CPT slug in the future, just visit the page once.
If your slug could change dynamically, then you can make use of flush_rewrite_rules(), but you need to use it carefully - it shouldn't be called on every page load, but could be used by a periodic cron job, or on plugin activation / deactivation, depends of the case.

Related

Wordpress automatically created a redirect that I need removed

I made an edit to a page on a Wordpress site that included changing its parent. Unfortunately I was not paying attention and this move somehow changed the permalink/slug which I saved before noticing. In addition Wordpress automatically created a 301 redirect to go from the original URL to this new URL. I went back in and changed the permalink/slug back to the original but that redirect still stands so it stills sends the page to the new url it created rather than keeping it to updated option (updated meaning me typing in the original slug).
I am looking for a way to remove the 301 redirect, but do not know where to start.
The original URL: https://www.synergex.com/rev11
The newly generated URL: https://www.synergex.com/products/synergy-de-rev11-licensing-faq
I also tried simply creating a brand new page using the original slug/permalink /rev11 but it gets changed to /rev11-2 so it seems like it it must remember the /rev11 somewhere.
I am hoping there is a way to do this through the WP Dashboard and am open to suggestions. This site does have the Redirection plugin installed but I've checked through the lists and this redirect was not created or listed in it. I also tried to add a redirect to do the opposite (redirect from the new URL to old) but of course it just created a loop that timed out.
EDIT 1.2: You probably have a problem with your old permalinks not getting flushed properly. But do not worry the great people at Wordpress got a function for that called flush_rewrite_rules();
Remove rewrite rules and then recreate rewrite rules. This function is
useful when used with custom post types as it allows for automatic
flushing of the WordPress rewrite rules (usually needs to be done
manually for new custom post types). However, this is an expensive
operation so it should only be used when necessary.
Source # https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
Add the following to the top of your function.php:
<?php
/**
* Removes a function from a specified filter hook.
* More about flush_rewrite_rules(); # https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
*/
flush_rewrite_rules();
/*
* Remove rewrite rules and then recreate rewrite rules.
* More about remove_filter(); # https://developer.wordpress.org/reference/functions/remove_filter/
*/
remove_filter( 'template_redirect', 'redirect_canonical' ); ?>

add_action() in wordpress uses php file name as first parameter. What does it mean?

Recently I am trying to understand the codebase of a custom wordpress plugin.
Some syntax of add_action() looks confusing to me.
add_action( 'after_plugin_row_wc-smart-cod/wc-smart-cod.php', array( $this, 'add_warning' ) );
I know the first parameter of add_action() is the name of the action to which the function 'add_warning' of $this object is hooked.
But in the above example the first parameter is the name of a php file, instead of an action name.
What does it mean ?
Thanks.
The first parameter to an action is a string. Generally it is something like wp_footer or before_send_email but there really isn't any restrictions on it. For instance, in my company's code, we often use slashes to give the action a (fake) namespace such as company/plugin-name/class/action but this is 100% arbitrary from WordPress's perspective.
Back to your example, however, there actually is a specific pattern from WordPress for that specific hook which you can see here. Every plugin in WordPress has an "entrance" or "plugin" file that boots up the entire plugin. Because most plugins live in a sub-folder, it is often plugin-name/plugin-name.php or plugin-name/index.php but it is ultimately up to the plugin author.
Most people use that specific action to add special messages to their own plugin's row in the general listing of plugins. The plugin you mentioned is using it to give a warning to users with a very specific version of the plugin installed.

Wordpress re write rule

Lets say I have a URI thats portfolio_categories/guttersspouting/
How could I rewrite a function to change it too "products"
ok as requested this matches the uri "product" exactly. This is not advisable if releasing code to a third party, as if they create a page called product, it will follow this rule.
Btw I am assuming you mean post_type = page (will also work for posts, see the post_id= in the add_rewrite_rule? You need to change this to the post id of the page you want to redirect to.
add_action('init', 'new_rewrite_rule');
function new_rewrite_rule(){
// matches product exactly, product/aproduct will fail, etc
// you need to add your own post_id into the function below.
add_rewrite_rule('^product$','index.php?page_id=12','top');
}
You will need to flush the rewrite rules for this to work (go to the permalinks setting page and just hit the save button)
code version of flush rewrite rules, not a good idea to have it running on wp_loaded, should be on a activation hook really but for testing this will do:
function new_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'new_flush_rewrite_rules' );
Just to note you can also do this manually in the post edit screen.

Completely custom page in WordPress generated by a plugin

I am writing a WordPress plugin that has an AJAX element to it - blocks of HTML are fetched by the front end from the plugin using AJAX.
I am having difficulty joining up the pieces here, and I suspect it is just a terminology issue. How would I implement a page completely provided by the plugin?
The content of the page will be HTML - the plugin can generate this in response to POST or GET parameters.
There needs to be a route to this page. The route does not have to be user-friendly or a REST style - just some kind of URI that gets to the plugin. Is there perhaps a way to register a custom page with an arbitrary name, without having to create it as a WP post?
I would like all this to be self-contained in the plugin, so should not involve the administrator having to create posts or pages, or have to add anything to the theme.
Ideally I would avoid any URLs that go into the wp-admin directory. End users don't belong in here.
I would strongly suggest referring to https://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side
You need to have a php script in your plugin directory that returns what you request, and you need to determine that url at run time for reference in your ajax. The above link gives an example for enqueuing and using wp_localize_script() to provide the url for your custom php script.
wp_enqueue_script( 'ajax-script',
plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
// in JavaScript, object properties are accessed as
// ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => plugins_url( '/php/myapi.php' ));
Your javascript file will be included on every page and will listen for events on the page which require a block of HTML, as you've described it.
Your file myapi.php then needs to take a request, probably using a query string, get the appropriate content from the wordpress api, and respond with said content, which your javascript will put into place.
To have access to the wordpress api as well though, you have two options:
Force wordpress to run starting with your file, by including wp-load.php. This is probably not the cleanest way.
Set up a custom page or slug to direct to your plugin.
I would advise the second option, and advise a slug, in which case you may find this post helpful: wp_rewrite in a WordPress Plugin
From Jason's comment, based on the link above:
The rewrite rules are mentioned a lot, but are really a distraction -
they just help to make URLs look more "friendly", which was not an
objective here. The key is: register a custom GET parameter; look for
that parameter early in the page rendering process; if you find the
parameter is set, then output/echo stuff and die(). There are a
number of hooks that can be used to look at the parameters, chosen
dependin on how much you want WP to set up and process first.

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

Resources