Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To get Page url in learnPress ? #527

Open
bhaveshdarji386 opened this issue Oct 24, 2022 · 2 comments
Open

How To get Page url in learnPress ? #527

bhaveshdarji386 opened this issue Oct 24, 2022 · 2 comments

Comments

@bhaveshdarji386
Copy link

Hello,
I am trying to get current page url. but I am not able to get I tried many ways to get id or url but not succeed
global $post;
echo $post->ID;

global $wp_query;
echo $wp_query->post->ID;

global $wp;
echo home_url( $wp->request )

not able to get id

global $wp;
echo home_url( $wp->request );

global $wp;
echo add_query_arg( $wp->query_vars, home_url() );

global $wp;
echo add_query_arg( $wp->query_vars, home_url( $wp->request ) );

www.host.com/wp-json/lp/v1/lazy-load/course-curriculum

I am getting this url but not getting browser one it should be something like

http:/host.com/courses/cambridge-igcse-physics-paper-2-mcqs-extended/quizzes/4-physics-2021-may-june-23

Please help me to find this solution I am new in wordpress.
Your response will be highly appreciate.
Thank You in advance ^_^

@rajaeinet
Copy link

Hi
If you want to get a page permalink in wordpress, you can use the get_permalink() (+) function and pass page ID to it:

<?php
global $post;
echo get_permalink( $post->ID );

But in learnpress, if you be on a lesson page, the $post->ID returns the course page ID instead of the lesson page ID!
In this case, you can call learn_press_get_current_url() function from learnpress plugin. Like below:

echo learn_press_get_current_url();

Good luck :)

@rajaeinet
Copy link

Also you can see the function source code here:

function learn_press_get_current_url() {
static $current_url;
if ( ! $current_url ) {
$url = untrailingslashit( $_SERVER['REQUEST_URI'] );
if ( ! preg_match( '!^https?!', $url ) ) {
$siteurl = trailingslashit( get_home_url() /* SITE_URL */ );
$home_query = '';
if ( strpos( $siteurl, '?' ) !== false ) {
$parts = explode( '?', $siteurl );
$home_query = $parts[1];
$siteurl = $parts[0];
}
if ( $home_query ) {
parse_str( untrailingslashit( $home_query ), $home_query );
$url = add_query_arg( $home_query, $url );
}
$segs1 = explode( '/', $siteurl );
$segs2 = explode( '/', $url );
if ( $removed = array_intersect( $segs1, $segs2 ) ) {
if ( $segs2 = array_diff( $segs2, $removed ) ) {
$current_url = $siteurl . join( '/', $segs2 );
if ( strpos( $current_url, '?' ) === false ) {
$current_url = trailingslashit( $current_url );
}
}
}
}
}
return $current_url;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants