Wordpress, PHP, URL Encoding Issue - php

Wordpress provides a function called "the_permalink()" that returns, you guessed it!, the permalink to a given post while in a loop of posts.
I am trying to URL encode that permalink and when I execute this code:
<?php
print(the_permalink());
$permalink = the_permalink();
print($permalink);
print(urlencode(the_permalink()));
print(urlencode($permalink));
$url = 'http://wpmu.local/graphjam/2008/11/06/test4/';
print($url);
print(urlencode($url));
?>
it produces these results in HTML:
http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http%3A%2F%2Fwpmu.local%2Fgraphjam%2F2008%2F11%2F06%2Ftest4%2F
I would expect lines 2, 3 and 5 of the output to be URL encoded, but only line 5 is so. Thoughts?

According to the docs, the_permalink prints the permalink vs returns it. So, urlencode isn't getting anything to encode.
Try get_permalink.
[EDIT]
A little late for an edit, but I didn't realize the print counts were such an issue.
Here's where they're all coming from:
<?php
print(the_permalink()); // prints (1)
$permalink = the_permalink(); // prints (2)
print($permalink); // nothing
print(urlencode(the_permalink())); // prints (3)
print(urlencode($permalink)); // nothing
$url = 'http://wpmu.local/graphjam/2008/11/06/test4/';
print($url); // prints (4)
print(urlencode($url)); // prints (5)
?>

the_permalink() echoes the permalink
get_the_permalink() returns the permalink so it can be assigned to a variable.
(same goes with most functions in WordPress: the_something() has a get_the_something() to return value instead of echoing it)

#Jonathan has the reason why, and the way you should deal with it in WordPress (ie. use the right function for the job).
Here is how to fix it when there isn't a function that returns a string:
ob_start();
the_permalink();
$permalink = ob_get_clean();
print(urlencode($permalink));

Related

Simple Shortcode (Wordpress) does not work

This is my "code":
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
Now when I want to use it in a post, all I get is:
[whoop]
As you can see I am very new and unused to it so maybe the answer is really simple, maybe I've just miss a thing in advance.
I both checked defining the function in functions.php and also in content.php
For shortcode function echo is required.
Please check with below code
function whoop_function(){
$responseData = "whoop whoop!";
echo $responseData;
return true;
}
add_shortcode('whoop', 'whoop_function' );
put your code in themes/function.php files and remove from content.php as function is duplicated so function already defined PHP error occurred.
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
and add your shortcode [whoop] in any page content section.
if u use do_shortcode('[whoop]'); then echo it like below.
<?php echo do_shortcode('[whoop]'); ?>
Your approach is correct. I think you are using it in a template. Just need to use it as mentioned below :
In files:
<?php echo do_shortcode('[whoop]'); ?>
In admin pages or posts:
[whoop]
Fetch the content like this:
$post_content = get_post(get_id_by_slug('short')); // assuming that you have already defined get_id_by_slug function as you are using it in your code and "short" is a slug for post you are fetching content
$content = $post_content->post_content;
echo do_shortcode( $content );
You are fetching content without considering the short code. Update your code as above.
Hope it will work for you.

Put the output of one PHP function into another in Wordpress

I am using a plugin that outputs data with a simple tag reference
<?php echo $property['building_brochure']; ?>
This outputs the reference number for an uploaded pdf which in this example is 352.
I want to return the actual link for this, the following works
<?php echo wp_get_attachment_url( 352 ); ?>
However, I want the combine these so on the single page template for the entry the relevant PDF is called i.e.
<?php echo wp_get_attachment_url( 'building_brochure' ); ?>
Is there a straightforward way of doing this?
You need to place the function as input into the other function the way Spectre-d has shared in comments.
i.e
<?php echo wp_get_attachment_url( $property['building_brochure'] ); ?>
However if you face issues,like not correct attachment_URL, then it could be Type issue i.e. first function might be outputting string and not integer as expected by attachment_url function in that case I recommend:
<?php
$pdf_id = (int)$property['building_brochure'];
echo wp_get_attachment_url( $pdf_id );
?>

how to print the content in wordpress php by passing the string

how to print the content in wordpress by passing the string php.I have written the following code but it print all the content including image.I want it to print only particular text.
<?php
$content = get_the_content('Read more');
print $content;
?>
This depends on whether or not you're in the Loop - if you are then your code should work - see more here: https://codex.wordpress.org/The_Loop
However, if you are outside the loop and want to pass in a post ID as a paramter, you can refer to this post as everything is explained very well in there: https://wordpress.stackexchange.com/questions/51741/get-post-content-from-outside-the-loop
$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;
Use the_excerpt for only showing small content with read more
<?php $short_desc= get_the_excerpt();
echo substr($short_desc,0,200);?>

site_url with path and custom variable

I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()

Using subsrt() with method the_title()

I'm trying to substring the title which it is called via the method the_title() .
Here are 2 things I did to do it but they both failed.
first try: <?php echo substr(the_title(),1,15) ?>
second try: <?php $new_title = the_title() ; echo substr($new_title, 1,15) ?>
They both didn't work. when I used the second try I still get the full title.
Note: I'm trying to implement this on a Wordpress script, also it's for my php practice.
Thanks in Advance.
The function the_title() does not return the title as string but outputs it by default.
Use the_title('','', 1) which makes it return the title or the alternative function get_the_title() instead.

Categories