I use a php-script within a plugin on a Wordpress-site to geocode an user-supplied address. After this I would like to visualize the point on a leaflet map. In order to do so I wanted to use the built-in functions from the leaflet-map plugin. I tracked down the class for this in the class.map-shortcode.php-file: Leaflet_Map_Shortcode. This class provideds the function shortcode. This is also added to the shortcode in Wordpress (Lines 135 onwards):
'leaflet-map' => array(
'file' => 'class.map-shortcode.php',
'class' => 'Leaflet_Map_Shortcode');
foreach ($this->_shortcodes as $shortcode => $details) {
include_once $shortcode_dir . $details['file'];
add_shortcode($shortcode, array($details['class'], 'shortcode'));
My Intention was using this in a straightforward way:
<?php
... some code for geocoding...
$coords = array("lng" => 11, "lat" =>43 );
$myMap= new Leaflet_Map_Shortcode;
$myMap->shortcode($coords);
?>
But nothing happens (i.e. nothing is displayed). So this leads me to several questions:
Why does this not work?
What's the best way of debugging this code?
Is there a better solution to my problem?
Turns out it is very easy due to a hint in the comments:
$mapPrint = $myMap->shortcode($coords);
echo($mapPrint);
Related
This is probably super simple but I just cant seem to figure it out.
How can I pass the current Wordpress page title into this code?
Below is a snippet from Formidable Forms WP plug-in which basically prints statistics from forms within my website.
In this case, the # of entries for a specific form (55jqi) and a specific field(50) are displayed on the pages, showing how many other people also submitted that form.
Im trying to skip needing to update each page (4,380 pages) with the stats output snippet.. and instead call the current page into the stats display code, to show stats for that particular page being viewed.. using an elementor custom post type template.
I need this:
echo FrmProStatisticsController::stats_shortcode(array('id' => '55jqi', 'type' => 'count', 50 => 'runtz'));
To work like this:
echo FrmProStatisticsController::stats_shortcode(array('id' => '55jqi', 'type' => 'count', 50 => 'single_post_title();'));
Replace the input area ‘Runts’ with the current page title, using
single_post_title()
Or similar.
Any help would be amazing!!
There is also a short code available which works the similarly.
[frm-stats id=55jqi type=count 50="Runtz"]
Formidable Forms Plugin shortcode and php page for reference: https://formidableforms.com/knowledgebase/add-field-totals-and-statistics/#kb-field-filters
You are using single_post_title function in a wrong way. By default, this function will echo the output but in the shortcode, you need to return the output.
This function accepts 2 param: 1: $prefix 2: $display
You need to pass $display as false, which will tell the function to return the value.
so you'll have to make a call like this `single_post_title( '', false )``
and your shortcode call will be like this:
echo FrmProStatisticsController::stats_shortcode(
array(
'id' => '55jqi',
'type' => 'count',
'50' => single_post_title( '', false ),
)
);
I need to update values in all posts of a wordpress installation on a regular base. I was gooing to use shortcodes to insert the request into the wordpress post. Then use a custom functions.php that holds all the variables that need to be updated from time to time.
I got it working. Somehow but not the way I intended to use it. I'm a total beginner. Please consider this when answering my questions.
I want to have a function that reads what comes after honda_ and displays the correct value in wordpress without having to create a separate shortcode for each variable.
When entering [honda_link] wordpress should display the value from honda_link. When entering [honda_longlink] the value from honda_longlink variable should get displayed. I don't want to create a shortcode for each value.
I came up with this as a working solution...
// Honda
function honda() {
$honda_link = 'www.honda.com';
$honda_longlink = 'http://www.honda.com';
$honda_free = 'Free';
$honda_new = '23.688 $';
$honda_mileage = '00';
return $honda_link;
}
add_shortcode('neu', 'honda_link');
I tried some approaches by using an array but it ultimately failed all the time. I also tried it with if statements but wasn't able to get the right value displayed.
Someone willing to help a noob? I think I need to see a working example in order to understand it. The code snippets I have been looking at (that do something similiar but not the same I want to achieve) did confuse me more than they helped me.
I came up with this / Which works in a way but... This isn't very comfortable to use.
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
shortcode_atts(array(
'var1' => 'default var1',
'var2' => false,
'var3' => false,
'var4' => false
), $atts);
if ($atts['var2'])
return 'honda2';
else if ($atts['var3'])
return 'honda3';
else if ($atts['var4'])
return 'honda4';
else
return 'honda1';
}
So now when using:
[HONDA var1="novalue"][/HONDA]
[HONDA var2="novalue"][/HONDA]
[HONDA var3="novalue"][/HONDA]
[HONDA var4="novalue"][/HONDA]
it shows:
honda1
honda2
honda3
honda4
and so on.
Is there a better way to achieve the intended goal from post #1 ? Any way I could import the $variables from 1st post in bulk for example?
I don't have a working WP setup right now to test, but could you try this:
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
$atts = shortcode_atts(array(
'model' => 1,
), $atts);
$myHondas = [1 => 'honda1', 'honda2', 'honda3'];
return isset($myHondas[$atts['model']]) ? $myHondas[$atts['model']] : 'unknown model id';
}
And use it with [HONDA model="1"], [HONDA model="2"]
I need to hide the URLs of downloads in wordpress posts. I have found a great script to do this but it is not a plugin. I have installed the script and created a function to include it. I am not a pro with php at all.
However the script has a line to normally call it:
<a href="<?php downloadurl('http://yourdomainname.comdownloadables.zip','veryspecials'); ?>" >Your Downloadables</a>
I am not able to place this directly in posts so I am trying to make a shortcode for it, but I am getting stuck. The shortcode that I have is:
function secshort_func($atts, $content = null) {
extract(shortcode_atts(array(
"linkurl" => '#Download_Does_Not_Exist',
"linktitle" => 'Download',
), $atts));
return '<a href="<?php downloadurl(' .$linkurl. ','veryspecials'); ?>" >' .$linktitle. '</a>';
}
add_shortcode( 'secdown', 'secshort_func' );
I am getting errors when trying to run this, and through a process of elimination I know that it is from this part of the return code:
"<?php downloadurl(' .$linkurl. ','veryspecials'); ?>"
After searching the internet for solutions and trying everything that I can think of, I am completely stuck.
Any help would be very much appreciated - it is driving me crazy being stuck on such a little thing!
A few observations along with the answer:
Format your code. It makes life much easier when troubleshooting. Good indenting is huge (see below for your code, formatted).
Don't use cryptic / abbreviated function names. Type them out so that you create self documenting code.
It's better to not use extract. There are some who say it's OK, but it can create confusing code that is hard to troubleshoot because you don't know where a variable came from. It's preferable to explicitly set the variables. (In your case, because you use them only once, it's simplest to just reference them in the array form - $atts['link_url'])
You can call the function, but it has to be concatenated into the string (see below). Your code (and the other answer), are passing php into the string, rather than calling the function and passing the result into the string.
Formatted code, with answer:
// Use a clearer function name. No need for "func", that's implied
function download_link_shortcode($atts, $content = NULL) {
// Declare $defaults in a separate variable to be clear, easy to read
$defaults = array(
"link_url" => '#Download_Does_Not_Exist',
"link_title" => 'Download',
);
// Merge the shortcode attributes
$atts = shortcode_atts( $defaults, $atts );
// Concatenate in the results of the `download` function call....
return '' . $atts['link_title'] . '';
}
add_shortcode( 'secdown', 'download_link_shortcode' );
Try using double outer quotes and escape inner single quotes like this:
return "<a href=\'<?php downloadurl(\'" . $linkurl . "\',\'veryspecials\'); ?>\' >" .$linktitle. '</a>';
I should say that I am only starting my journey with PHP so probably that's why I can not do such a simple task.
I have got a simple php code which I need to use as a shortcode. I got no problem in make code for shortcode but what I cant understand is how to insert that php function inside. The php function I am trying to insert is:
$( element ).airport([ 'moscow', 'berlin', 'stockholm' ]);
And as a shortcode I made a such code:
function home_words_shortcode() {
return $element .airport([ 'moscow', 'berlin', 'stockholm' ]);
}
add_shortcode( 'home-words', 'home_words_shortcode' );
I did a research online and found what I thought similar to my code and tried to convert to circumstances but nothing seems to work.
Could anyone help me please.
Not sure why you want to add this script as a shortcode, but this could be a way:
function home_words_shortcode( $atts ) {
$atts = shortcode_atts( array(
'element' => '#default-element',
'cities' => '',
), $atts, 'home-words' );
$cities = json_encode(array_filter(explode(',', $atts['cities'])));
return "
<script>
jQuery('{$atts['element']}').airport($cities);
</script>
";
}
add_shortcode( 'home-words', 'home_words_shortcode' );
It works with a shortcode like:
[home-words element="#element" cities="moscow,berlin,stockholm"]
I'm not sure if I understand you, but this is not a php function:
$( element ).airport([ 'moscow', 'berlin', 'stockholm' ]);
As I can see, it's a jquery code, maybe you have problems with the selectors, you can get all the knowledge rigth here. And a example
I hope this will be useful for you.
PD: I'm not a native english speaker, so, be comprehensive please :)
This should be fairly simple for anyone familiar with MediaWiki, but it's stumping me for me because being me.
I'm working on a skin, and I need to show the currently logged in user's name in a top bar - let's assume in plain text, for simplicity's sake, with changes via CSS.
Initially, I was planning on using the automatically generated one used in the personal tools bar, but since the generating line in the skin is
<?php $this->renderNavigation( 'PERSONAL' ); ?>
, it's inseparable from there. I looked in User.php and found its generation line:
public function getUserPage() {
return Title::makeTitle( NS_USER, $this->getName() );
}
So, I figure I might be able to use this function somehow, but I have very little knowledge of PHP, and am unsure how.
EDIT: It appears that this is used for the generation in the personal tools line itself, but again, I'm not sure how to adapt this.
$personal_urls['userpage'] = array(
'text' => $this->username,
'href' => &$this->userpageUrlDetails['href'],
'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
);
Could I duplicate this into a separate function, and make something like the following?
<?php $this->renderNavigation( 'USERNAME' ); ?>
You can use this code:
<?php echo htmlspecialchars($this->getSkin()->getUser()->getName()); ?>
Or, as the User class has a __ToString() magic method:
<?php echo htmlspecialchars($this->getSkin()->getUser()); ?>
Sources :
The SkinTemplate class in MediaWiki code documentation
The User class in the same documentation
CurrentUsers
http://www.mediawiki.org/wiki/Extension:CurrentUsers
GetUserName
http://www.mediawiki.org/wiki/Extension:GetUserName
Modify these extension for your needs
If you indeed just want the username inserted somewhere into the skin HTML, this should do it:
<?php echo htmlspecialchars( $this->username ); ?>