What to do if a shortcode already exists - wordpress

I'm writing a Worpress plugin that defines a shortcode.
What happens at shortcode registration if there's another shortcode already registered with that same name ?
What is the practice ? Is there a possibility to find the conflicting plugin so as to warn the user that he should deactivate it to be able to activate yours ??
Thanks.
if ( !shortcode_exists( 'myshortcode' ) ) {
add_shortcode('myshortcode','mycallback');
}
else ?????

The best practice is allways to prefix the name of your shortcode in order to avoid naming collisions. You can use a particular prefix in relation with your plugin name or activity. You can see some guideline in the plugins development handbook here : https://developer.wordpress.org/plugins/the-basics/best-practices/#prefix-everything
My personal practice is to process with a personal name spacing format. It could give something like:
add_shotcode('company_plugin_shorcode');
Hope it helps

Asking the user to disable another plugin just because you can't register your shortcode with the name you want might not be the best idea for a variety of reasons.
Other things you could try instead:
Name your shortcode something unique. For example, WooCommerce's shortcodes are all prefixed with woocommerce_: [woocommerce_cart], [woocommerce_checkout], et cetera.
You could register an alternative shortcode if the name you want to use is already taken.
If you ask me, I'd go for #1.

Related

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.

Display the same author name for all users in WordPress?

For a Wordpress organization website, I want all the posts to have by default the same author name (the organization name). How can I achieve this behavior?
There are multiple possibilites.
1) Simplest: You only create one author and share the login within the organization
2) You simply do not display the author on your post - why would you do that? It is probably obvious that your organization is the publisher of these pages anyway.
3) Add the following custom code within your Theme or Plugin:
add_filter('the_author','its_my_company');
function its_my_company() {
return 'Organization Name';
}
https://developer.wordpress.org/reference/hooks/the_author/
Your best best is to modify your theme or child theme to display a specific user or name wherever the template does so. In your single.php file, look for the_author() or get_the_author(). Then use something like get_user_by() to pull a specific user, or else hard code the value (less ideal, but an option).
Your other option is to manually set it each time, which I could exactly define as making anything "default." Even so, the option exists.

WPML same slug(url) different language

I have this site :
example.com/watches (watches is a custom post type)
There are two available languages : EN(default language), GR.
When i change to GR (while on page example.com/watches)
it redirects to example.com/el/watches which is right.
When i am on example.com/watches/rolex and i try to change language it redirects to example.com/el/taxwatches/rolex-el but i want it just to be example.com/el/watches/rolex/.
taxwatches is my custom taxonomy.
rolex-el is the slug of the term inside that taxonomy.
Has anyone experienced an issue like this before?
I've tried to save the permalinks again and check my WMPL settings but i can't see something wrong.
EDIT 1 : If i manually go to example.com/el/watches/rolex it will work fine.
Both example.com/el/watches/rolex and example.com/el/taxwatches/rolex-el work.
EDIT 2: From what i understand WPML takes the slugs, is there a way to change it so it takes the names?
Ok so for anyone having problems with multi language sites - but want to have the "same slugs" in the urls (will never be same slugs - but going to look like that) read the below.
WPML uses slugs which are created from the wordpress core structure - you can't have two slugs with the exactly same name. If default language is EN and you have a secondary, in my case GR, then one slug is going to be "myslug" and the other "myslug-gr" (you can customize this "-gr" through WPML settings).
In functions.php you can filter the "wpml_ls_filter" function which is fired when your Language switcher is created.
Code as below :
add_filter('icl_ls_languages', 'wpml_ls_filter');
function wpml_ls_filter($languages) {
foreach ($languages as $lang_code => $language) {
$mTempString = $languages[$lang_code]['url'];
echo $mTempString; // HERE
// If "tax" is found in that string, replace it with "" - remove it.
if (strpos($mTempString, "tax") !== false) {
$languages[$lang_code]['url'] = str_replace("tax", "", $mTempString);
}
}
return $languages;
}
The above echo is going to show you the url that is created (and you can manipulate it) of each language button when pressed whenever you visit a page/post.
I haven't wrote a complete solution because that really depends on what you want to do. For example the above code helped me achieve to remove "tax" if found in the url.
This answer is providing an alternative way to achieve multiple languages with same slug
Polylang with Polylang Slug these 2 plugins can do
https://wordpress.org/plugins/polylang/
https://github.com/grappler/polylang-slug/
You can use my plugin, it is based on the newest Polylang Pro module and does what you need: https://github.com/lufton/polylang-share-slug

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'); ?>

Change management in WordPress

I have a beginner question. What is the best way to address the change management issues in WordPress? I have an all-pages WordPress installation. Suppose name of some event or an entity changes from A to B, then I have to go to all the pages to make that change. Is there any better way of doing it? Like externalization or something.
Or the way similar to how WordPress handle blog name using bloginfo() function. You change blog name at one place and it is reflected everywhere.
Thanks,
Paras
If a URL on your site changes, it is always wise to leave a redirect to the new page. This will help your visitors and search engines. If you create redirects, it doesn't matter too much if you still have a link to the old address in one of your posts. There will probably be a plugin for this, but I don't know which one.
If you really want to keep all links pointing to the latest version, you could replace them with shortcodes that are evaluated to the real URL. <a href="[linkto postid=123]"> would then result in <a href="/2010/08/05/some-post">. I think this is doable, but I don't know whether a plugin already exists for this.
You can also use this technique to replace short snippets, like your company name. The Shortcode API is really easy:
// [company_name]
function replace_company_name($atts) {
return "My Awesome Company";
}
add_shortcode('company_name', 'replace_company_name');
// More generic
// [replace r='company_name']
// [replace r='company_motto']
function do_replacement($atts) {
$replacements = array(
'company_name' => 'My Awesome Company',
'company_motto' => 'A Company so Awesome even you would want to work here!',
);
return $replacements[$atts['r']];
}
add_shortcode('replace', 'do_replacement');
You can hardcode the strings in your plugin code, or you could create a Wordpress options page where users can add and edit new shortcodes.

Resources