Getting page excerpt by page ID wordpress - php

I trying to make a shortcode to output specific page excerpt.Did research but there is no output on the code that I structured.
function page_shortcode( $atts )
{
$page_id = 173;
$page_data = get_page( $page_id );
$the_excerpt = $page_data->post_excerpt;
echo $the_excerpt;
}
add_shortcode( 'page_shortcode_excerpt', 'page_shortcode');

The page excerpt activation code is okay! but the get_page function you used in your shortcode is deprecated. you can check here enter link description here for more information.
Try putting below code for your shortcode:
function page_shortcode( $atts )
{
$page_id = 173;
$page_data = get_post( $page_id );
$the_excerpt = $page_data->post_excerpt;
return $the_excerpt;
}
add_shortcode( 'page_shortcode_excerpt', 'page_shortcode');
Also when you edit the page with id=173 from your admin panel, from screen options check excerpt check box at the top of the page and check if the excerpt field has any value or not. If not excerpt on front end won't show anything.
Hope this will help you!

I just needed to activate excerpt on page using this line of code
add_action( 'init', 'add_excerpts_to_pages' );
function add_excerpts_to_pages() {
add_post_type_support( 'page', 'excerpt' );
}
reference : excerpt on page

Related

Add any username as link prefix and show data from that specific user’s profile on wordpress pages – with no login required

Example: www.example.com/johndoe/sample-page/
User with username “johndoe” has
Name field value of “John Doe”
E-mail field value of “johndoe#email.com”
Facebook field value of “facebook.com/johndoe”
Address field value of “Main street 47 ”
Show this values on Sample Page via shortcode
[userdata username={read_from_url} field=email]
Once opened, save this username in a cookie and redirect the visitor:
www.example.com/sample-page/ --> www.example.com/username/sample-page/
Add that USERNAME to all internal links to avoid redirects (from there on every active link on the website to contain the prefix)
Live example with the same functionality on another wordpress site: https://eqology.com/15499949/webshop/our-products-en/omega-3-en/
(Check CTRL+U to see all internal links contain the username/id prefix)
I've tried creating a rewrite tag and a rule for it to be put in a url param
add_rewrite_tag('%username%', '([^&]+)', 'username=');
Then use URL Params shortcode
[urlparam param='username'] but I know it's not suppossed to work this way :D
I've managed to solve it :D :D :D Received $1000 offers to create this plugin and some failed in delivering it; Got so much from the wp community I'll share it without comments.
functions.php
add_action('init', 'eqid_url_rewrite');
function eqid_url_rewrite() {
add_rewrite_rule('([^/]*)/(.+)/?$', 'index.php?eqid=$matches[1]&pagename=$matches[2]','top');
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter( 'query_vars', 'eqid_query_var' );
function eqid_query_var($vars){
$vars[] = 'eqid';
return $vars;
}
add_action( 'wp', 'eqid_cookie' );
function eqid_cookie(){
$eqid = get_query_var('eqid');
if($eqid) {
$eqbp = get_user_by('login', $eqid);
if (isset($eqbp->user_login))
setcookie('eqid_cookie', $eqid, strtotime('+90 days'), '/');
}
$prefix_slug_wslash = "bp/";
if(($_COOKIE['eqid_cookie']) && (!$eqid) && $_SERVER['REQUEST_URI'] !== "/" && (strpos($_SERVER['REQUEST_URI'],$prefix_slug_wslash) == true) && (strpos($_SERVER['REQUEST_URI'],$_COOKIE['eqid_cookie']) == false) && !is_admin()) {
wp_redirect( "https://".$_SERVER['HTTP_HOST']."/".$_COOKIE['eqid_cookie']."/".$_SERVER['REQUEST_URI'] );
exit;
}
}
function eqid_shortcode(){
$eqid = get_query_var('eqid');
//$eqid = $_COOKIE['eqid_cookie'];
if($eqid)
$eqbp = get_user_by('login', $eqid);
return $eqbp->user_login;
}
add_shortcode( 'eqbp_id', 'eqid_shortcode' );
function eqid_shortcode_nev(){
$eqid = get_query_var('eqid');
//$eqid = $_COOKIE['eqid_cookie'];
if($eqid)
$eqbp = get_user_by('login', $eqid);
return $eqbp->first_name." ".$eqbp->last_name;
}
add_shortcode( 'eqbp_nev', 'eqid_shortcode_nev' );
author.php
if( ! defined( 'ABSPATH' ) ){ die(); }
$eqid = substr($_SERVER['REQUEST_URI'], 1, -1);
if ($eqid)
setcookie('eqid_cookie', $eqid, strtotime('+90 days'), '/');
wp_redirect( home_url() );
exit;
Above script works for all sub-pages of $prefix_slug_wslash = "bp/";
You can remove that and it will work for all pages.
After opening link like: example.com/username cookie is set and the page will redirect to home. I've added this to header top bar with a shortcode or you can put it in child theme header.php also
//Header Site badge tag [shortcode]
function eqid_headersitebadgetag(){
$eqid = get_query_var('eqid');
if(!$eqid)
$eqid = $_COOKIE['eqid_cookie'];
if($eqid) {
$eqbp = get_user_by('login', $eqid);
if (isset($eqbp->user_login))
return "<strong>".$eqbp->first_name." ".$eqbp->last_name."</strong> <em>(".$eqbp->nickname.")</em>";
}
return;
}
add_shortcode( 'eqbp_sheader', 'eqid_headersitebadgetag' );
For posts I needed the author slug in the link:
Permalinks structure: /%author%/%postname%/
function _eqid_shortcode(){
return get_the_author_meta( 'login' );
}
add_shortcode( '_eqbp_id', '_eqid_shortcode' );
This is more like having an affiliate name masked in a pretty url which will be kept across navigation and you can show affiliate specific info on pages with shortcodes without the need of login from other users.

Wordpress Custom search page other than search.php

I want to include custom Hashtag search page in my Wordpress theme.And I have the following code in functions.php file
function hashtag_template( $template ){
global $wp_query;
$hash = get_query_var('hashtag');
if( !empty($hash) ){
return locate_template('hashtags-search.php');
}
return $template;
}
add_action( 'template_include', 'hashtag_template' );
and I have also added a hashtag-search.php template
but when I am searching with www.mysite.com/?hashtag=string, it does not shows that(hashtag-search.php) search page.Where may be the problem?

Run comments_template() function in Wordpress Plugins

I include the comments on the page afterwards with ajax, required code is in the plugin, but comment_template() function does not work when the request goes plugin file, Ajax request is successful, but there are no comments.
Codes that go to Ajax are as follows.
add_action('wp_ajax_nopriv_comment_update_get','comment_update_get');
add_action('wp_ajax_comment_update_get', 'comment_update_get');
function comment_update_get(){
global $withcomments;
$withcomments = 1;
$post = get_post($_POST['post_id']);
comments_template();
die();
}
From the Code Reference:
Will not display the comments template if not on single post or page,
or if the post does not have comments.
If you take a look at the source code, you'll notice that the comments_template() function checks whether we're currently on a post or a page, or if the global $post object is set up.
So:
function comment_update_get(){
// Set up our required global objects
global $post, $withcomments;
$withcomments = 1;
$post = get_post( $_POST['post_id'] );
// Load the comments template
comments_template();
// We're done here
wp_die();
}
add_action( 'wp_ajax_nopriv_comment_update_get','comment_update_get' );
add_action( 'wp_ajax_comment_update_get', 'comment_update_get' );

how to remove read more after call the_content function in wordpress?

I want to remove read-more link after calling the_content() function of wordpress. I want only to show content of each post in a loop and read-more link is redundant.
I try this code but read-more link remains after post content:
add_filter( 'the_content_more_link', 'disable_more_link', 10, 2 );
function disable_more_link( $more_link, $more_link_text ) {
return;
}
where I can find the code that adds this read-more link after the content?
You can use a filter to remove the read-more link from content. I have updated your filter. Please try it, It's working fine.
function disable_more_link( $link ) {
$link = preg_replace( '|#more-[0-9]+|', '', '' );
return $link;
}
add_filter( 'the_content_more_link', 'disable_more_link', 10, 2 );
More info
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

How to add values to a Wordpress template file programmatically?

I'm new to Wordpress & PHP, so kindly excuse the naivety of the question, if any.
I'm building a plugin where I need to select values from the database and create a new HTML page with the values. I'm using a custom template file.
What I've done till now
Extracted the values from database
Load & display my own template in my plugin file
add_action( 'init', 'leb_add_endpoint' );
function leb_add_endpoint()
{
add_rewrite_endpoint( 'result', EP_PERMALINK );
}
add_action( 'template_include', 'leb_render_template' );
function leb_render_template($default_template) {
//some code removed for brevity
if ( ! is_singular() || !isset($wp_query->query_vars['result']) )
{
return $default_template;
}
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
$default_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $default_template;
}
The content of my-custom-template.php is as follows
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
?>
The page gets displayed without any problem. All I want is to insert $sample_result and other similar results pulled form database into my-custom-template.php
I need to generate dynamic pages based on values pulled from DB. So each time, the final page created might be different. E.g. if one hits www.example.com/sample-post/result, a page will be shown with values pulled from the DB. If one hits www.example.com/another-sample-post/result, a different page will be shown with different values. Both these pages will have the same design, only a few values will be different. This is what I'm trying to achieve.
How can I do that? Please help me. I'm stuck. :(
You need to define "result" in query vars.
Use EP_ROOT Endpoint Mask ( result/{var} is located in the root )
Inside template_include hook, you can find result value inside $wp_query object.
I've already tested the code
// Add a new var to query vars
function result_add_query_vars( $vars ){
$vars[] = 'result';
return $vars;
}
add_filter( 'query_vars', 'result_add_query_vars' );
// Add endpoint
function result_add_endpoint() {
add_rewrite_endpoint( 'result', EP_ROOT );
}
add_action( 'init', 'result_add_endpoint');
// change the template
function result_render_template($template)
{
global $wp_query;
if ( array_key_exists( 'result', $wp_query->query_vars ) ) {
$result = get_query_var('result');
$new_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $new_template;
} else {
return $template;
}
}
add_action( 'template_include', 'result_render_template' );
Now you can retrieve the query var in your custom template
/*
* Template Name: My Custom Template
*/
$result = get_query_var('result');
echo $result;
Well why don't you use $wp_query Inside your my-custom-template.php
<?php
/* Template Name: My Template*/
global $wp_query;
echo '<pre>';
print_r($wp_query); // Use this in case you want to see what else do you have with you.
echo '<pre/>';
// Now you can use $wp_query to build your dynamic query at run time.
// This will allow you to perform task at run time
?>
To Retrieve Meta
If you have saved something as a meta then
<?php
$meta_values = get_post_meta( $post_id, $key, $single );
?>
To Retrieve Child Post
If you want to retrieve child posts then
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
// Do your stuff here such as below
the_title();
the_content();
endwhile;
else:
echo 'No Post Found';
endif;
?>
1) Write this function in function.php in your active template.
function leb_render_template() {
//some code removed for brevity
//$sql = 'YOUR_QUERY_CODE'
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
return $my_template;
}
add_action('wp_ajax_leb_render_template', 'leb_render_template');
add_action('wp_ajax_nopriv_leb_render_template', 'leb_render_template');
2) Call function in your custom template.
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
$result = leb_render_template();
print_r($result); // Print Your function output.
?>

Categories