how to use get_permalink() in init hook - wordpress

I want to get current url of page in init hook. but they return nothing. my code is.
function mship_access_level() {
global $wpdb;
global $post;
$slug = basename(get_permalink());
echo get_permalink();
exit;
}
add_action('init', 'mship_access_level');

you can try this below code for permalink
<?php
function mship_access_level() {
global $wpdb;
global $post;
$slug = basename(get_permalink( get_the_ID()));
echo get_permalink( get_the_ID());
exit;
}
add_action('init', 'mship_access_level');
?>

<?php
function mship_access_level() {
global $wpdb;
global $post;
$slug = basename(get_permalink($post->ID));
echo get_permalink($post->ID);
exit;
}
add_action('init', 'mship_access_level');
?>
please try this one also.

Related

Getting slug from url in wordpress

How can i get slug from url in wordpress. For example my url is https://ww.test.com/test-page/test-post/data-to-get how can i get data-to-get from url in wordpress. I tried following code
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
what is wrong here
There is a built-in function for that in wordpress: url_to_postid.
<?php
global $wp;
$current_url = $wp->request;
$post_id = url_to_postid($current_url);
$slug = get_post_field( 'post_name', $post_id );
echo $slug;
You can achieve your output using below methods.
by using Global post variable.
<?php
global $post;
$post_slug = $post->post_name;
?>
using PHP
<?php
global $wp;
$current_url = $wp->request;
echo substr($current_url , strrpos($current_url , '/') + 1);
?>
Suppose URL: https://ww.test.com/test-page/test-post/data-to-get
OUTPUT: data-to-get
I have checked and it's work for me.
Let me know if this works for you.

$wpdb->insert with WordPress shortcode

I am using WordPress shortcode with $wpdb->insert. It works, but it runs TWICE.
function f_registrazione()
{
$output = '';
if(!$_POST['txtNome'])
{
$output .= "do something in html";
} else {
$output .= "do something else in html";
}
global $wpdb;
$wpdb->insert( mg_nomi, array('des_nome' => $_POST['txtNome']) );
return $output;
}
add_shortcode( 'registrazione', 'f_registrazione' );
This function is written in a custom file linked to them's functions.php
What's wrong? I've already tried different solutions, none work.
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
wp_reset_postdata();
wp_reset_query();

Display Post Terms and add link

I've created this shortcode to display my terms (custom taxonomy) on specific post (custom post types) :
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'forme_juridique',array('fields'
=> 'names') );
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
echo '<li>' . implode( '</li><li>', $terms) . '</li>';
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
I am trying to add a link (url) on my terms in order to redirect to the term page but I didn't succeed yet.
Any help ?
Use get_term_link()function for this:
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms($post->ID, 'forme_juridique');
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
foreach($terms as $term){
$term_link = get_term_link($term, 'forme_juridique');
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);

How to print excerpt and content in the head?

I want to create a simple plugin that prints the content and the excerpt in the head section. Below is the code I tried but didn't worked:
function content_excerpt() {
if( is_single() ) {
the_content();
the_excerpt();
}
}
add_action( 'wp_head', 'content_excerpt' );
Try following:-
function content_excerpt() {
if( is_single() ) {
//use global $post variable
global $post;
echo $post->post_content; //echoing post_content
echo $post->post_excerpt; //echoing post_excerpt
}
}
add_action( 'wp_head', 'content_excerpt' );

How can i reset a query in a custom wordpress metabox

I'm working on this plugin for wordpress and i am stuck on a query that won't be reset.
In the following function:
function WPSM_artists_autocomplete(){
$response = array();
query_posts('post_type=artist&posts_per_page=-1');
if (have_posts()) : while (have_posts()) : the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'artist-icon');
$image_url = $image_url[0];
$response[] = array( get_the_ID() , get_the_title() , null, '<img src="'.$image_url.'" />'. get_the_title());
endwhile; endif;
wp_reset_query();
// Write JSON file
$output = json_encode($response);
$data = WPSM_CACHE_DIR."/data.json";
$fh = fopen($data, 'w') or die("can't open file");
fwrite($fh, $output);
fclose($fh);
// Return JSON url
echo WPSM_CACHE_URL."/data.json";
}
I use a query_posts to populate a metabox. But the wp_reset_query(); doesn't seem to work properly. This affects all other metaboxes and post related option. The global $post variable is set to the latest value of this query, and not the default value of the posts edit page.
I'd love to hear how to solve this plugin. Could use everything to get me in the right direction. Thanks in advance!
Cheers,
Ronny
I came across this today and found a fix.
You will need to store the original $post before starting a new loop and then at the end of your function you will need to set it back.
Before you function assign $post to a temporary variable.
$original_query = $wp_query;
Then at the end of your function set it back.
$wp_query = $original_query;
wp_reset_postdata();
Not sure if the above works in your case as I was using a custom query.
I have posted my code below so you can have a look.
global $wpdb;
global $post;
$originalpost = $post;
$querydetails = "
SELECT *
FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'projects'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querydetails, OBJECT);
if ($pageposts) {
foreach ($pageposts as $post) {
setup_postdata($post);
$postID = get_the_ID();
echo '<option value="';
echo $postID . '"';
foreach ($meta as $m) {
if ($postID == $m) echo ' selected="selected" ';
}
echo '>';
echo the_title();
echo '</option>';
}
}
echo "</select>";
$this->show_field_end($field, $meta);
$post = $originalpost;

Resources