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

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';

Related

how to make short code working in 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>

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]');
}
?>

hatom-entry errors on the Google snippet test

Almost sure that I'm not the first one that has this question but when I test my (WordPress) page on Google Snippet Test Tool I got hatom-enty errors like:
hatom-feed
hatom-entry:
Fout: At least one field must be set for HatomEntry.
Fout: Missing required field "entry-title".
Fout: Missing required field "updated".
Fout: Missing required hCard "author".
I found some tutorials about this but that is for standard WordPress themes. I'm using Inovado from ThemeForest and I can't figure out in which file I have to edit this data.
Someone familiar with this?
I also got problems with snippet review... Good in testresults but doesn't show up in de Google Search Results. Don't know why...
You can Add this code to the function.php file in your theme's directory and it will solve the problems.
//mod content
function hatom_mod_post_content ($content) {
if ( in_the_loop() && !is_page() ) {
$content = '<span class="entry-content">'.$content.'</span>';
}
return $content;
}
add_filter( 'the_content', 'hatom_mod_post_content');
//add hatom data
function add_mod_hatom_data($content) {
$t = get_the_modified_time('F jS, Y');
$author = get_the_author();
$title = get_the_title();
if(is_single()) {
$content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
}
return $content;
}
add_filter('the_content', 'add_mod_hatom_data');
The code contains 2 functions. The first function will use the WordPress filter hook for “the_content” to add the class of “entry-content” to the article. To add in the other essential hAtom fields, the second function will add a short sentence to the end of your post article, which contains updated time, post title and author, with required microdata.

targeting title in wordpress post

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.
I started out by using the 'the_title' filter hook, and calling this function.
function add_button_to_title($title)
{
global $post;
if(is_single())
{
return $title.'googly googly';
}
return $title;
}
The problem is, the links on the side bar apparently also use 'the_title', as I saw my text showing up in the side bars as well, which led me to:
if(is_single() && in_the_loop())
But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses 'the title' filter. So finally I have:
if(is_single() && in_the_loop() && ($post->post_title == $title))
The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes...It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.
Any help is appreciated!
Ying,
There isn't really a good solution except, as ShaderOp said, requiring theme modification. Your solution will work for the most part. The only exception is if the theme developer has changed the query in a page. I'd say this is probably a good enough solution that it'll cover more than 95% of the cases you'd run into.
I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with other post titles on the page (in sidebar, menu) also getting filtered.
function add_button_to_title( $title ) {
global $post;
if( is_single() && $title == $post->post_title ) {
return $title . 'googly googly';
} else {
return $title;
}
}
Wouldn't it be easier to keep the original version of your add_button_to_title function, but instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?
For example, somewhere in your theme's single.php, instead of this:
<h3 class="storytitle">
<?php the_title(); ?>
</h3>
Use this:
<h3 class="storytitle">
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php echo add_button_to_title(the_title('', '', false); ?>
</a>
</h3>
today I ran into a similar problem. the_title gets called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.
Additionally, I added a boolean flag which is set to true using the 'the_content' filter. So the title gets changed until the content is displayed. This way, I ensure that sidebars/widgets are not affected (e.g. Thematic theme has a default widget with links to pages - here the other conditionals would not be helpful as get_the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the 'the_title' call for the page/post to enable the boolean flag.
function myplugin_adjust_title($title, $id) {
global $myplugin_title_changed;
if ($myplugin_title_changed) {
return $title;
}
if (in_the_loop() && is_page('myplugin') && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'myplugin_adjust_title', 10, 2);
function myplugin_adjust_title_helper_content($content) {
global $myplugin_title_changed;
$myplugin_title_changed = true;
return $content;
}
add_filter('the_content', 'myplugin_adjust_title_helper_content');

How do I get a list of items in the Wordpress media library on a plugin options page?

I'm writing a Wordpress plugin which injects a grid of images just above the footer on all frontend pages. The application is to display sponsor's logos. I'd like to harness the WP Media Library since the logos are already uploaded for use on the 'sponsorship' page and in posts.
Essentially I'm stuck at accessing the media library interface on the plugin's options page. All of the legwork is done in terms of creating the options page, using the action hook to place content on frontend pages from the plugin, etc. What I need now is to be able to display all the files in the media library in a list on the options page, and provide a checkbox or something to allow the user to select certain files for insertion above the footer.
The Media Library API seems to be aimed at people writing themes or media plugins. Help understanding what to make use of would be great!
I think you'd be much better off adding your own column into the existing media library, rather than try re-coding it yourself;
function my_media_col($cols)
{
$cols['my_col'] = 'Footer';
return $cols;
}
add_filter('manage_media_columns', 'my_media_col');
function handle_my_media_col($name, $id)
{
if ($name !== 'my_col')
return false;
$in_footer = get_option('in_footer', array());
?>
<input type="checkbox" name="in_footer[]" value="<?php echo $id; ?>" <?php checked(in_array($id, $in_footer)); ?> />
<?php
}
add_action('manage_media_custom_column', 'handle_my_media_col', 10, 2);
Then just hook onto the load-upload.php (the library page) and save changes when POST'ed;
function save_my_col()
{
if (!isset($_POST['in_footer']))
return false;
$in_footer = $_POST['in_footer'];
if (is_array($in_footer))
$in_footer = array_map('absint', $in_footer); // sanitize
else
$in_footer = array();
$in_footer = array_merge(get_option('in_footer', array()), $in_footer);
$in_footer = array_unique(array_filter($in_footer));
update_option('in_footer', $in_footer);
}
add_action('load-upload.php', 'save_my_col');
Note this is just an example, and I may have one or two typos.
UPDATED:
My code example should store an array of IDs in the options table, under the key 'in_footer'.
Put in practice, you can get all media items marked 'in footer' like so;
$query = new WP_Query(array('post__in' => get_option('in_footer', array()) ));
if ($query->have_posts()): while ($query->have_posts()): $query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; endif; ?>

Resources