Wordpress custom permalink shortcode - php

Working on a Wordpress site and where I need to have a post with php content.
I figured out that this is only possible with a plugin or a shortcode in the functions.php
Googled around, tried a lot but it isn't working out, so i definitely doing something wrong.
code I have in functions.php:
function anniversary_link($text) {
$url = the_permalink();
return "<a href='$url'>$text</a>";
}
add_shortcode('permalink', 'anniversary_link');
And the post I have:
and the result I get when clicking the link:
The shortcode has to reference to the single.php page and al the static code's references to the single.php page is just:
<?php the_permalink() ;?>
Is this te 'correct' way to use href on a post (is there a better/cleaner way to get this working)?
Edit
Updated my code thanks to nathan Edit edit: in functions.php
function anniversary_link( $atts ) {
$atts = shortcode_atts( array(
'text' => '',
), $atts, 'permalink' );
$url = get_permalink();
return '' . $atts['text'] . '';
}
add_shortcode('permalink', 'anniversary_link');
And how I use this short code inside a post (I think that I incorrectly use the shortcode):
Result:
Edit Edit
This is how I call the dynamic anniversary post:
<?php echo get_posts(array( 'category_name' => 'Anniversary' ))[0]->post_content ;?>
(inside the header)
solution thanks to nathan

Reading through the code you've posted I see three issues.
The way you're accessing the 'text' attribute.
The function you're using to get the permalink.
The way you're inserting your shortcode into your content.
Shortcode Attributes
The first parameter of a shortcode callback should be an array of attributes, not a single string. Naming your parameter $text has no bearing on the value and won't pull the text attribute of your shortcode.
Change the name of your parameter from $text to $atts and set a default value for the text attribute. Setting a default value is a good practice with shortcodes and can be done using the shortcode_atts() function.
Return vs. Output Functions
The second issue is your use of the_permalink(). the_permalink() doesn't return a permalink but outputs it directly instead. As such you can't assign it to a variable.
The new function
function anniversary_link( $atts ) {
// Set defaults where needed
$atts = shortcode_atts( array(
'text' => '',
), $atts, 'permalink' );
// Replace the_permalink().
// Given the level of simplicity it doesn't need it's own variable.
$url = get_permalink();
// Put together a new return statement.
// Various ways this could be formatted. I went with something clear and easy to understand.
return '' . $atts['text'] . '';
}
Usage
In your code you're using the shortcode inside the href attribute of a link. The shortcode returns a full link, not a URL, and therefore shouldn't be inside another a tag.
Example:
[permalink text="My Link Text"]
// Outputs My Link Text

Related

Why is wordpress function returning values outside of my variable

I have a function in the functions.php file in my wordpress theme folder. This function creates a link to a php file where you can download the ics file for selected wordpress post in the frontend.
function ics_maker() {
$ics_link = "<a href='https://intranet.local/wp/icsmaker.php?eventname=".the_title()."&eventort=".the_field('eventort')."&eventstart=".the_field('eventstart')."&eventende=".the_field('eventende')."&eventext=".get_the_content()."'>Download</a>";
return $ics_link;
}
add_shortcode( 'shortcode_icsmaker', 'ics_maker');
If I add this shortcode with
[shortcode_icsmaker]
I'm getting this:
TitleofICSAddressOfICS24.03.2022 13:0024.03.2022 17:00 Download
The download link is a working link but have empty vars inside href excepct eventext:
https://intranet.local/wp/icsmaker.php?eventname=&eventort=&eventstart=&eventende=&eventext=Lorem%20123
It seems like the_title(), the_field("eventort"),... return themselves outside of the $ics_link variable, but why?
From the WordPress function reference (https://developer.wordpress.org/reference/functions/the_title/):
the_title( string $before = '', string $after = '', bool $echo = true )
Display or retrieve the current post title with optional markup.
By default it echoes the title. You need to either explicitly ask for returning the title instead: the_title('', '', false) or use the get_the_title() method, just like you did with get_the_content().
Instead of ACF's the_field(), you have the get_field() function, which returns the field value instead of echoing it (https://www.advancedcustomfields.com/resources/the_field/).

How do I use a PHP Short Code from custom field as an href link on Wordpress?

On my wordpress page (which I will use as a template to duplicate) I need to use a custom field 'dropbox' as an href url.
I created a PHP Code Snippet (shortcode) to use as a replacement for the url: [xyz-ips snippet="dropbox"]
Here is the PHP within the shortcode:
<?php the_field('dropbox'); ?>
Here is the code on the page:
download the documents
This short code will not pass the url from custom field to the href. Any thoughts?
Thanks in advance.
Your shortcode should be working if it's as you describe, but you should note shortcodes need to return values, not echo them. You didn't post the code you're using to register it, so I can only assume you're echoing content instead of returning it.
With that said,
I would just make the shortcode echo out the whole entire link, that way you have a bit more granular control over the whole thing:
add_shortcode( 'xyz-ips-link', function( $atts ){
extract( shortcode_atts( array(
'snippet' => 'dropbox',
'text' => 'Download the Documents',
'target' => '_new',
), $atts ) );
$link = sprintf( '%s', get_field( $snippet ), $target, $text );
return $link;
});
This will let you just use [xyz-ips-link] anywhere in your content,
or <?php echo do_shortcode( '[xyz-ips-link]' ); ?> in your page templates.
It also gives you more granular control over the content of the link, such as [xyz-ips-link snippet="dropbox" text="Some Alternative Text"].
You'll also note I'm using get_field() instead of the_field(). WordPress (and ACF) both have get_ functions that return a variable for you to use, and the_ functions which get and output the variable by default.
You can see an example of this code here, Note:I don't have ACF installed, so I replaced the href attribute)
Hi Please try it may work for you .
echo do_shortcode('dropbox');
Try it
$dropdown = get_the_field('dropbox');
echo do_shortcode($dropbox);
OR
$dropdown = get_field('dropbox');
echo do_shortcode($dropbox);
So it turns out there is a specific way to call url custom fields (ACF). I inserted the below code into a PHP Code shortcode. This worked like a charm!
<?php
$link = get_field('dropbox');
if( $link ): ?>
<a class="button" href="<?php echo $link; ?>" target="_new">download documents</a>
<?php endif; ?>

Creating a custom Wordpress Shortcode with 2 arrays

I'm trying to created a custom shortcode in wordpress but I can't get it working.
Here's the code I have so far:
function wp_test_video($atts) {
extract(shortcode_atts(array(
'X' => ''
'Y' => ''
), $atts));
return '[iframe src="http://www.example.com/test.php?X='.$atts['X'].'&Y='.$atts['Y'].'"]';
}
add_shortcode('test', 'wp_test_video');
Everytime I try and insert it into my functions.php file my site just breaks.
EDIT: The shortcode is now working but it seems to be acting differently from the same code inserted without the shortcode.
Here is an image of the compiled code from the shortcode in a post by itself:
http://i.imgur.com/4lM8jC5.jpg
Here is what a post looks like when using the shortcode to generate the same code:
http://i.imgur.com/gxk6k3C.jpg
The video is embedding but it's breaking out of the article wrapper causing none of widgets or comments to work (also appears to mess up the search bar at the top).
You forgot a comma after your first item in your array and forgot to assign the shortcode_atts to a variable. Don't use extract(), it's deprecated.
function wp_test_video($atts) {
$atts = shortcode_atts(
array(
'X' => '', // <-- This one
'Y' => ''
),
$atts);
return '[iframe src="http://www.example.com/test.php?X='.$atts['X'].'&Y='.$atts['Y'].'"]';
}

wordpress shortcode attributes not being passed

I've created a short code that I'm trying to pass an attribute into, but I don't seem to be receiving the value on the other end.
Here's what I've got below;
function list_display($atts) {
extract( shortcode_atts(
array(
'pageName' => 'No Page Received',
), $atts )
);
echo $pageName;
add_shortcode('list-briefings-display', 'list_display');
}
and the shortcode being used is
[list-display pageName="My Page Name"]
and I'm running a require_once from functions.php
require_once ( TEMPLATEPATH. '/includes/list-display.php' );
But what I'm seeing on the screen is 'No Page Received', can anyone think of something I might've missed?
There is more content being generated by the shortcode, which I have't included, that's rendering fine. So it just seems to be something to do with how I've passed the attribute.
Any help is appreciated.
function list_display($atts) {
$atts = shortcode_atts( array(
'pagename' => 'No Page Received'
), $atts );
extract($atts);
echo $pagename;
}
add_shortcode('list-display', 'list_display');
You'll probably want to use "return" instead of "echo" if you're using the shortcode within pages and posts.. Echo could cause it to send output to the screen a little too early and won't end up exactly where you may be expecting it to be.
There was also a little formatting issue in your code that I've corrected, mainly trying to use add_shortcode() from within the very same function you're trying to reference. I also changed the first parameter of add_shortcode() to the shortcode you were trying to use in your example.

Wordpress: include content of one page in another

How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.

Categories