Insert a Plugin Setting Value as Shortcode Attribute - wordpress

I wrote a plugin.
One of the plugin's settings is a URL/page.
I want my client to be able to continue to use the theme's page builder, to create any buttons that will link to the URL that is entered in the plugin's settings.
The client could enter the URL manually for every button they create, but this would be tedious, and will become a massive pain should the URL in the plugin's settings change.
So, I want to be able to use the plugin's URL setting value as the URL attribute of a third-party button shortcode.
Is this possible? Something like:
[button url="{get the plugin's URL setting}"][/button]

Yes definitely possible.
You would need to add the shortcode functionality to your plugin. Best to use a unique name to avoid conflicts (not 'button').
in your plugin php file add the function and register it with the add_shortcode function like so:
function shortcodeName_function( $atts ) {
// add attribute handling and get the 'url' parameter
$output = '<button a href="'. $url . '" class="button">';
return $output;
}
add_shortcode( 'shortcodeName', 'shortcodeName_function');
Now u can use now a short code with this name, when the plugin is activated.
[shortcodeName]
See the link of the wordpress documentation for more info on parameter handling: wordpress shortcode api
EDIT: ah sorry, I think I missed the point a bit. What you could do is - store the plugin url setting in a cookie and check for the cookie value in the short code.

When shortcode have not attribute url you can provide default settings url in shortcode return output.
Like this
add_shortcode( 'button', 'shortcode_function_button');
function shortcode_function_button( $atts ){
$attr = shortcode_atts( array(
'url' => get_option( 'button_default_url' ) , // get your setting default url if shortcode have not
), $atts ); // attr url="http://example.com" then it use default
// setting url
return 'Button';
}
If already build shortcode in plugin or theme then find shortcode callback function and change atts in this way.
If probleming in find third party shortcode callback name check in your plugin file all registered shortcodes with callback.
global $shortcode_tags;
print_r( $shortcode_tags );
// show all shortcodes with callback
/*Array
(
[embed] => __return_false
[wp_caption] => img_caption_shortcode
[caption] => img_caption_shortcode
[gallery] => gallery_shortcode
[playlist] => wp_playlist_shortcode
[audio] => wp_audio_shortcode
[video] => wp_video_shortcode
[button] => button_shortcode
)*/
If do you not want to any change in thired party shortcode and manage in your plugin
Remove shortcode previously declared thired party plugin and add in your plugin top of file.
remove_shortcode('button'); // https://developer.wordpress.org/reference/functions/remove_shortcode/
Recreate same shortcode tag after remove but use own callback name and work same as thired party shortcode like this
add_shortcode( 'button', 'your_own_callback' );
function your_own_callback( $atts, $content ){
$attr = shortcode_atts( array(
'url' => get_option( 'button_default_url' ) , // Use same atts thired party using atts
), $atts );
return button_shortcode( $attr, $content); // Use thired party callback function name.
}

Related

How do you get wordpress plugin shortcode to $_GET url parameters

I have a plugin on a page update-data which has a shortcode on the page of [updatedata]
here is what an update row looks like
<td>UPDATE</td>
Here is what the plugin code is
<?php
/*
plugin name: deano plugin update
description: deano test database to update data into books table
author: Dean-O
*/
$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path.'/wp-load.php');
error_log("here"); // echos to log file
error_log(var_dump($id)); // echos a blank line to the log file
error_log("here 2"); // echos to the log file
function deanoupdatedata($atts, $content = null ) {
$a = shortcode_atts( array(
'id' => 'id'
), $atts );
return '<a id="' . esc_attr($a['id']) . '</a>'; // never displays
}
add_shortcode('updatedata','deanoupdatedata');
?>
How can I get the url parameter id in the plugin when I click on update link?
Try if($_GET['id']) instead of isset and istead of error_log use var_dump to get the exact value
It is a bad practice. You should pass the data to a shortcode using the shortcode's attributes.

Dynamic permalinks when creating a post in WordPress

I have a custom post type named houses. Inside my custom post type I have several custom fields which I created using ACF.
What I need to do is to change the permalink when I create a new post.
I would like to use the code and title fields to customize the permalink:
//code + post title
4563312-house-example-1
I'm developing a plugin which controls everything.
Is there a way to intermediate the creation of a post to update its permalink?
Thanks.
After some research, I found an answer related to wp_insert_post_data.
Using wp_insert_post_data, I couldn't get the custom field value, and to achieve that, I had to use another action, save_post instead.
function rci_custom_permalink($post_id) {
$post = get_post($post_id);
if($post->post_type !== 'houses') return;
$code = get_field('code', $post_id);
$post_name = sanitize_title($post->post_title);
$permalink = $code . '-' . $post_name;
// remove the action to not enter in a loop
remove_action('save_post', 'rci_custom_permalink');
// perform the update
wp_update_post(array('ID' => $post_id, 'post_name' => $permalink));
// add the action again
add_action('save_post', 'rci_custom_permalink');
}
add_action('save_post', 'rci_custom_permalink');
PS: Since all these fields are required, I didn't need to check if they are empty or not.
For reference about save_post action:
Plugin API/Action Reference/save post

WP Plugin Development - Conditional CSS on shortcode

I'm currently developing a plugin for wordpress.
My plugin-content gets fired on defined shortcode. I pass a parameter to the shortcode to make some conditionals. I am able to load JS conditionally, but I also need to load CSS conditionally.
Lets say I use the shortcode: [myshortcode css="dark"]
I make a database query and inject the wanted css.
Is this somehow possible?
I have read some threads about it. I know it is because the Code fires after head is loaded. I wasn't able to find a solution.
What options do I have?
Thank you!
Probably you are searching for these functions:
has_shortcode()
get_shortcode_regex() - here you can find nice example, which is close to your request
You can check your post, page or custom post type on hook by add_action ( 'wp', 'yourCustomFunction' ) and test if your get_post_field ( 'post_content' ) contains specified shortcode and conditionally enqueue CSS file based on specified attribute.
There is a better way. You can simply register your css and scripts with wp_enqueue_scripts by wp_register_style or wp_register_script and then enqueue the registered scripts from your shortcode. You will have opportunity to enqueue scripts based on certain condition there.
Here is a code example.
/**
* Register scripts
*/
function my_plugin_scripts() {
wp_register_style( 'dark-css', $css_path );
wp_register_script( 'script-name', $js_path, array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_scripts' );
/**
* My Shortcode
*/
function my_plugin_shortcode( $atts ) {
$atts = shortcode_atts( array(
'css' => 'default'
), $atts );
if ( $atts['css'] == 'dark') {
wp_enqueue_style('dark-css')
}
// do shortcode actions here
}
add_shortcode( 'shortcode-id','my_plugin_shortcode' );
If I understand your question correctly, you could do something like this to load one stylesheet or the other:
function aw_shortcode_function($atts) {
$a = shortcode_atts( array(
'css' => 'light', // this is the default value
), $atts );
$colorTheme = $a['css'];
if ($colorTheme == 'dark') {
// load/enqueue/get/do whatever based on the css="dark" shortcode attribute
}
}

Wordpress shortcode without installing plugin

How would I create a shortcode such that when a URL is pasted in a Wordpress blog post in Visual mode it get parsed and converted to an iFrame?
Here's an example: http://en.support.wordpress.com/shortcodes/getty-images/
That guide illustrates that the user can paste a URL to a GettyImage in the Visual mode and it gets converted to an iFrame.
You can create a shortcode just by editing your functions.php of your wordpress theme:
function convert_to_iframe_shortcode( $atts ) {
extract( shortcode_atts( array(
'url' => 'http://standarsite.com'
), $atts ) );
return
'<iframe src="'.$url.'"></iframe>';
}
add_shortcode( 'iframeurl', 'convert_to_iframe_shortcode' );
Then in your blog post, you can use shortcodes like this
[iframeurl url="http://yoursite.com"]

Syncing custom fields between 2 languages (with WPML)

I'm using WPML (Wordpress multilanguage plugin) with custom post and fields (with Advanced Custom Fields plugin) and I have this "issue":
I've a custom post with a custom field (text), I enter text in the field and save. Now I go to the translated post and see that the same custom field is empty. Then the fields ar not synched. Notice that instead the Tag field is well synched between the languages.
Someone can help? Thanks
I don´t think the saved value of a custom field is synced by default. Only the name of the variable etc.
So if you have a custom field and wan´t it to have the same value on all languages, simply don´t add that custom field to other languages. Just have it on the main language.
Then in the template you can use this, to allways get the value from main language:
<?php the_field('fieldname',lang_page_original_id($post->ID));?>
Then add this to functions.php
function lang_page_original_id($id){
if(function_exists('icl_object_id')) {
return icl_object_id($id,'page', false, "MAIN LANGUAGE CODE EX: SV");
} else {
return $id;
}
}
Here are ACF docs: http://www.advancedcustomfields.com/resources/multilingual-custom-fields/
But it doesn't work as well as you may expect. Syncing is only "one way" from original to translated versions. See: https://wordpress.stackexchange.com/questions/181338/fixed-values-for-same-post-translations/214120#214120 for more details.
You will need WPML Multilingual CMS in order to use sync feature.
Hi use this in your function.php works 100%:
function sync_field_meta( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
// use this if u have muti custom post type
$posts_type = array('your_custom_post_type1', 'your_custom_post_type2', 'your_custom_post_type3', 'your_custom_post_type4');
if( ! in_array($post_type, $posts_type)) return;
$en = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'en' );
$fr = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'fr' );
// your acf key like (field_58136c9dc9963) you can check documention
$field = get_field('acf_key',$post_id);
if($en){
update_field('acf_key',$field,$en);
}
if($fr){
update_field('acf_key',$field,$fr);
}
}
add_action( 'save_post', 'sync_field_meta', 10, 3 );

Resources