how to make short code working in wordpress? - wordpress

I am trying to add short code in my code. Here its is
function my_shortcode_functionnn()
{
$i = 'Hiii ';
return $i;
}
add_shortcode('myshortcodeee', 'my_shortcode_functionnn');
Then I have used it like this
<h1>options <?php $sc='[myshortcodeee]'; echo $sc; ?></h1>
Also I tried like this
<h1>options [myshortcodeee]</h1>
but in my output it shows options [myshortcodeee] instead of options Hiii
So what is the problem I don't know. Help me.

You should use the function do_shortcode() so that the code make action:
<h1>options <?php echo do_shortcode('[myshortcodeee]'); ?></h1>

Related

how to use short code in a <?php ?> tag in .php file in wordpress

I am using "Profile Builder" plugin to show registration form on my site in wordpress.
It works when I add it in the text file in this format [wppb-register]. But I want it inside if else condition, if the user is logged in do not show this form otherwise display it???
How can I implement it.
[wppb-register] is the short code.
Maybe this one help:
if (! is_user_logged_in() ) {
echo do_shortcode('wppb-register');
}
You can use [wppb-register] like this -
<?php if(is_user_logged_in()){
echo "You are logged In";
}else{
echo do_shortcode('[wppb-register]');
}
?>
This code use to create a new page called Register (or something similar) we can use the [wppb-register] shortcode.
Read More Information about shortcode -
https://developer.wordpress.org/reference/functions/do_shortcode/
https://www.cozmoslabs.com/docs/profile-builder-2/shortcodes/
you can use
<?php echo do_shortcode('[wppb-register]'); ?>
See:
https://developer.wordpress.org/reference/functions/do_shortcode/
some thing like this:
<?php if(is_user_logged_in()){
// do some thing
}else{
echo do_shortcode('[wppb-register]');
}
?>

Proper Shortcode Usage in Template File

I have a two part shortcode, with opening and closing shortcodes, in a template file. See example below.
echo do_shortcode('[myshortcode][/myshortcode]');
I see from the Codex that I can do this:
echo do_shortcode('[myshortcode]' . '<div class="anyHTML">Any Text</div>' . '[/myshortcode]');
Is it possible/correct or will it work to do this instead?
echo do_shortcode('[myshortcode]');
$somePHP = possibleWordPressLoopCodeOrOtherPHP;
echo do_shortcode('[/myshortcode]');
I'm wondering about this to see if I can include PHP inside a two part shortcode, possibly a WordPress loop for a CPT or even other PHP code.
You need to have the whole shortcode block inside the do_shortcode function. So you could do something like this:
$text = some_code_or_function_that_returns_text_your_shortcode_can_act_on();
$sc_string = sprintf("[myshortcode]\n%s\n[/myshortcode]", $text);
echo do_shortcode($sc_string);

Is there a concise Wordpress function for building a page link from an ID?

I'm currently building links like this:
<?php echo get_the_title(111); ?>
I was building links like this using the WPML plugin (but steering away from it due to various reasons):
<?php icl_link_to_element(111); ?>
This builds a link similar to my first example.
So my question is is there a native Wordpress function that does this? I'm sure there must be, but cannot find the solution anywhere. I'm looking to reduce my markup...
Thanks!
EDITED WITH ANSWER
This is how I built my custom function:
function build_pretty_link($id,$link_title='') {
if($link_title=='') {
$link_title = get_the_title($id);
}
$link_url = get_permalink($id);
echo "{$link_title}";
}
WordPress give a function that print an anchor tag with the title and the url, but you have to be in a the loop (http://codex.wordpress.org/Function_Reference/permalink_anchor).
I suggest you to create your own function (the functions.php file in your theme is here for that).
You can do someting like that :
function vp_link_to($post_id) {
echo '<?php echo get_the_title($post_id); ?>';
}
get_permalink(x);
Where the ID of the page is x and wrap this in whatever you need, so
$id = 10;
$link = get_permalink($id);
echo 'Linked text';

Wordpress Pods: Index in Template

Is it possible to get a pod items index in the template? For instance, if I have a pods variable that has ten entries in it, could I make a template that renders each entry with its place in the list or, even better yet, if it's first/last as such:
Rendering item {#index} out of {#total} called {#title}
Note: I mean the index in the scope of the group of items being rendered. Not the posts ID or something else. If this doesn't exist it would be a great feature to have!
It's possible with PHP:
Total in this list: <?php echo $obj->total(); ?>
Or you can get the total (across all pages, if using pagination or limiting):
Total Found: <?php echo $obj->total_found(); ?>
Or you can get the current position in the loop (new in Pods 2.3):
Current Position: <?php echo $obj->position(); ?>
Or you can even do an nth check (CSS nerds know what I'm talking about in regards to how nth-child works):
<?php
if ( $obj->nth( 'even' ) )
echo 'even row';
if ( $obj->nth( 'odd' ) )
echo 'odd row';
if ( $obj->nth( '1n+3' ) )
echo 'you get the picture';
if ( $obj->nth( '3n+0' ) )
echo 'you get the picture';
?>
For info about nth, it just takes the same input as nth-child does in CSS: http://www.w3schools.com/cssref/sel_nth-child.asp
If you'd like these available as magic tags, please submit a feature request at http://pods.io/submit/
I have the same requirement. My scenario is that I need to detect first element so that I can display it in a slideshow. Inside template, I declare a global variable:
<?php global $counter;
if ($counter == 0) {
$class='active';
$counter++;
} else {
$class='';
}?>
Then I echo variable $class to where i want.
By this way, I cannot know totally how many row but It sold my problem.
PHP is deprecated in Pods templates. Now you should use Pods Frontier plugin or put PHP code in WP templates.

PHP getimagesize is not working when is called from a function in function.php (Wordpress)?

PHP getimagesize is not working when is called from a function in function.php.
function.php:
<?php
// Theme Options
require_once(TEMPLATEPATH . '/functions/admin-menu.php');
add_action('wp_head', 'theme_options', 'get_image_size');
function theme_options() {
// Initiate Theme Options
$options = get_option('plugin_options');
// If a logo image was uploaded then remove text from site title
if ($options['logo'] != NULL)
$remove_text = '-9999px';
else
$remove_text = 0;
?><style>
body {
background-color: <?php echo $options['color_scheme']; ?>
}
#header h1 a {
background: url(<?php echo $options['logo']; ?>) no-repeat scroll 0 0;
text-indent: <?php echo $remove_text; ?>;
}
</style><?php
}
function get_image_size() {
list($width, $height, $type, $attr) = getimagesize($options['logo']);
echo "Image width " .$width;
echo "<BR>";
echo "Image height " .$height;
echo "<BR>";
var_dump($width);
var_dump($heigt);
}
$options['logo'] is returning http://localhost/wordpress/wp-content/uploads/2010/12/logo4.png so the image is being displayed.
I also did var_dump to $width and $height but they didn't show up.
Any suggestions?
EDIT: I pasted the full code of functions.php. $options['logo'] works perfectly in the theme_option function so I don't know why it doesn't work in the get_image_size function.
$options['logo'] is undefined in your code. If it is defined outside of your function, it is not by default available inside of your function.
Please enable error reporting using ini_set('display_errors', 1) and error_reporting(E_ALL), when developing. This will make sure any errors are reported.
I you do not see any error messages, turn up error_reporting and display_errors.
$options['logo'] works perfectly in
the theme_option function so I don't
know why it doesn't work in the
get_image_size function.
As #Sjoerd said: $options is not defined in function get_image_size. It is only defined in function theme_options. That's what functions are about, they are a black box that know about their environment only from the arguments they receive. If you want to make the options visible in function get_image_size, you have to Initiate Theme Options in that function as well.
I found out how to fix it (separating the functions into different add_action statements):
add_action('wp_head', 'theme_options');
function theme_options {
...
}
add_action('wp_head', 'get_image_size');
function get_image_size {
...
}
The add_action only allows one function?
What was the problem?

Resources