How do I make my links in Wordpress not add itself to the existing URL? - wordpress

I'm guessing there is a simple solution to this, but I can't seem to get my phrasing right when searching for it, so I'll post it here.
I have some links that look like this in Wordpress:
<a target="_blank" href="<?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?>"><?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?></a>
Just regular links that I echo in my single template to create user homepage/facebook etc. The problem is when you click it, the link will just adds itself to the end of the URL:
Example:
wordpress.com/single
when clicking the link:
wordpress.com/single/www.homepagelink.com
Thanks for any help :)

My guess is that wordpress isn't adding anything. If you have the URL without preceding http:// in the custom field it is shown that way by the browser. If you check the generated source code with your browser you will find the code like this:
<a target="_blank" href="www.homepagelink.com">www.homepagelink.com</a>
Without http:// or other valid URL schema this is interpreted by the browser as a relative link and handled as such.
You can either add the http:// in the field value or you place a wrapper function in the functions.php of your theme to make sure it is always interpreted as URL regardless what was put in the field.
function my_field_link($id, $field) {
$value = get_post_meta($id, $field, true);
if (substr($value, 0, 7) == "http://") return $value;
return "http://" . $value;
}
Then you can call this function like this:
<a target="_blank" href="<?php echo my_field_link($post->ID, prefix.'hjemmeside'); ?>"><?php echo my_field_link($post->ID, prefix.'hjemmeside'); ?></a>
Now the link will always start with http://.
Note: If you expect to have other URL schemas in use (https, ftp, scp, etc.) you should adapt the function accordingly.

Related

what is the difference between get_header_image() and header_image() in wordpress

in the process of self learning from the official wordpress theme developement handbook
this page shows how to create headers and two different functions were used:
get_header_image()
header_image()
both return a string, which is the image URL.
is this just another confusing redundency ? or is there an actual difference between the two.
Semantic
dis·play
Make a prominent exhibition of (something) in a place where it can be easily seen.
the palace used to display a series of Flemish tapestries.
re·trieve
get or bring (something) back; regain possession of.
I was sent to retrieve the balls from his garden
How
While header_image() will echo out the header image URL, get_header_image() will not.
<?php
header_image();
get_header_image();
header_image() is a wrapper for get_header_image(). Mainly used on the front-end, the role is to escape and echo out get_header_image().
<?php
/**
* Displays header image URL.
*
* #link https://developer.wordpress.org/reference/functions/header_image/
*/
function header_image() {
$image = get_header_image();
if ( $image ) {
echo esc_url( $image );
}
}
A practical use case of get_header_image() would be inside a function.
WordPress use that get_ ... distinction for most of it's default functions, eg:
get_the_title() and the_title().
get_the_post_thumbnail() and the_post_thumbnail().
get_the_content() and the_content().
... etc.
I found the answer.
Someone posted it then deleted it quickly, but just for the sake of benefiting everyone who is looking for an answer:
header_image() will echo the URL without the need of using the php echo. While get_header_image() will also return the image URL but it doesn't echo it. You have to use the php echo for it.
NB: this isn't mentioned in the official documentation, unless am blind I challenge anyone to show me where it says echo in the official page, or where this distinction is explained.

Add Page Title to source key in url for use in a form - Wordpress Website

I am trying to add a waitlist button functionality to a page on my wordpress website. Basically I need to autopopulate the source key in the query string in a url with the page title.
More Details:
I have the button linking to a general waitlist form but I want the product information from the page title go into a text field. I can do this by using a source key in the url.
We have our product listed on a portfolio post here with a waitlist button: http://www.inventivewebdesign.com/renohifi/listings_portfolio/pass-labs-xa-160-8-monoblock-power-amps/
The button goes to a form here with a text field that auto populates with the source key in the url.
So if I use the link: http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=Pass%20Labs%20XA-160.8%20Monoblock%20Power%20Amps, The source key will populate the the product text field with "Pass Labs XA-160.8 Monoblock Power Amps". This all works great!
Now, I want to automate the code for the button so it always pulls the Page title as the source key so we don't have to manually enter in code for each button.
How can I get the page title to auto-populate the query string in the link url so that the link will be http://www.inventivewebdesign.com/renohifi/waitlist-request/?source={PAGE_TITLE}?
FYI - I am using the Visual Composer plugin for page layout and button creation.
UPDATE:
I am trying to use code like this:
<div class="vc_btn3-container vc_btn3-center">
<a title="Waitlist - <?php the_title_attribute(); ?>" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=<?php echo get_the_title(); ?>">
Add Me to the Waitlist
</a>
</div>
It is in the Wordpress editor so the code is not showing up as anything other than code. I want the title to show (I am trying two different wordpress calls for the page title). You can see this in the 2nd "Add Me to the Wishlist" Button on the first link above.
In JavaScript you can get the page title from the DOM using document.title, the value is encoded using the encodeURIComponent() function.
Your URL as a JavaScript string:
var url = "http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=" + encodeURIComponent(document.title);
I couldn't find any reference to the URL on the page to which you linked.
EDIT: To encode the title string using PHP you need to use the urlencode() function.
Template code:
<div class="vc_btn3-container vc_btn3-center">
<a title="Waitlist - <?php the_title_attribute(); ?>" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source=<?php echo urlencode(get_the_title()); ?>">
Add Me to the Waitlist
</a>
</div>
I did it by adding a shortcode instead:
function shortcode_waitlist( $atts ){
$pagetitle = get_the_title();
$link = '<div class="vc_btn3-container vc_btn3-center"><a class="vc_general vc_btn3 vc_btn3-size-lg vc_btn3-shape-rounded vc_btn3-style-modern vc_btn3-block vc_btn3-color-primary" href="http://www.inventivewebdesign.com/renohifi/waitlist-request/?source='.$pagetitle.'" title="Waitlist - '.$pagetitle.'">Add Me to the Waitlist</a></div>';
return $link;
}
add_shortcode( 'waitlist_button', 'shortcode_waitlist' );
Then just used [waitlist_button] in the page.
Thanks for your suggestions, they helped me get to where I needed to go.

Wordpress Redirect page

I search several times but I couldn't find nothing that really helps me.
Basically I have a very VERY simple Theme which is working very well.
I also have a database with some IP's and depending by this IP I need to redirect the guest to different pages.
So on my template index.php file I added:
if ($country == "br") {
echo "case1";
} else {
$location = bloginfo('template_url')."/index-nbr.php";
echo $location;
?>
<script type="text/javascript">
window.location= <?php echo "'" . $location . "'"; ?>;
</script>
<?php
}
I tried many different ways to redirect but none of them work. Could someone help me with this stuff?
Ok let me make your life easier.
Just install this plugin you will find a text box on the bottom of every page in PAGES section in Admin panel where you can put your desired link/URL of the page and that will redirect you to your inserted URL.
Page Link to Plugin

Way to get Twitter button, with count, with custom bit.ly URL, working?

I'm stuck. I posted this on WordPress.StackExchange and they suggested I try at WebApps.StackExchange, and they suggested I try here. So, apologies for the multiple posts if you follow all those!
I have a client blog using bit.ly pro to generate custom short urls (ie foo.co). I want to show the regular horizontal version of the Twitter button, with tweet-count, and have the link that goes to the post use their custom bit.ly pro url.
I have installed Joost de Valk's Bit.ly Shortlinks plugin, which successfully converts normal WP shortlinks (wp_get_shortlink()) to the custom Bit.ly pro URL elsewhere in the site, but Twitter seems to trump that and render everything with the default t.co domain instead.
I've looked at the suggestions from this question but using the # as the data-url doesn't work, and the suggested Twitter support pages don't seem to contain any info on how to get Bit.ly to work (though they say they're going to).
Here's the function I created to insert the button in my theme - any ideas on where I'm going wrong? this is used to insert the button both within the Loop and on single-post pages.
function tweet_this() {
global $post;
ob_start();
$tweet = wp_get_shortlink();
echo '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
return ob_get_clean();
}
In case it helps, this function does work, except it doesn't render the tweet-count:
function tweet_this() {
global $post;
ob_start();
$tweet = sprintf( __('%1$s %2$s'), $post->post_title, wp_get_shortlink() );
echo '<a class="tweethis" href="http://twitter.com/intent/tweet?text=' . urlencode( $tweet ) . ' via #clientname">Tweet this</a>';
return ob_get_clean();
}
Let me know if you need more info - and thanks in advance for any help you can throw my way!
Michelle
function tweet_this() {
global $post;
$tweet = get_permalink(); //replace with your code
$tweetmarkup = '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
echo $tweetmarkup;
}
This works for me, but I don't have the WPShortlinks installed, so I replaced it with the permalink. You should be able to replace the permalink with your wp_get_shortlink and it should work.

Link to registration page in Wordpress

There is
wp_login_url, wp_logout_url, but what what about registration url?
Is there standard way to get link to registration? I need to display a link to registration page with further redirect to previous page.
PS I am using my theme login.
The following will return the registration url:
<?php
echo site_url('/wp-login.php?action=register');
?>
UPDATE:
To get the registration url with a redirect to the current page use:
<?php
echo site_url('/wp-login.php?action=register&redirect_to=' . get_permalink());
?>
Since 3.6, there is now a func:
http://codex.wordpress.org/Function_Reference/wp_registration_url
<?php echo wp_registration_url(); ?>
You can override it with the register_url filter.
add_filter( 'register_url', 'custom_register_url' );
function custom_register_url( $register_url )
{
$register_url = get_permalink( $register_page_id );
return $register_url;
}
I know this is an old question, but for anyone picking it up use wp_register().
It automatically determines if you're logged in and supplies either a link to the admin section of the site, or a link to the register form.
It also respects the settings in Settings -> General -> Membership (Anyone Can Register?)
If I understand correctly you are asking for default Word Press registration page. That would be www.domainname.com/wp-signup.php
<?php echo wp_registration_url(); ?>
https://codex.wordpress.org/Function_Reference/wp_registration_url
2 points here
Make sure you have turn on the "Anyone can register" in the setting page
if you host in a subfolder, make sure you include
Without subfolder:
Register
OR
Register
With subfolder
Register
In case of hosting your wordpress site in a subfolder (e.g.: mysite.com/myblog) you also need to include your site's url as follows:
<?php echo get_site_url() . "/wp-login.php?action=register" ?>
--> http://mysite.com/myblog/wp-login.php?action=register
Otherwise you will be redirected to a non-existing page
--> http://mysite.com/wp-login.php?action=register
1st of all I'm a WP newbe.
wp_registration_url can redirect the new user back to the page he came from, buy without enforcing login on the way (I dont use auto login because i want the user to authenticate the mail).
i use instead (sample with Allow PHP in posts plugin):
Register
hope it helps...
You can use wp_registration_url() any where you want to add link to wordpress registration
to add the redirection url use apply_filters()
<a href="<?php wp_registration_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ); ?>" >click to register</a>

Resources