$wpdb->insert with WordPress shortcode - wordpress

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();

Related

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 use get_permalink() in init hook

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.

How to format Woocommerce tags cloud?

Im using following function to output Woocommerce tag cloud:
add_action( 'woocommerce_before_shop_loop_item', 'woocommerce_product_loop_tags', 5 );
function woocommerce_product_loop_tags() {
global $post, $product;
echo wc_get_product_tag_list( $product->get_id(), ', ', '<span class="tagged_as">' , '</span>' );
}
I would like to remove links from tags, comma between them and I need to add to each tag unique class selector.
Is it possible to do this? If so, how? Thanks!
OK, I done it by myself, any review is welcome.
Should I "escape" anything?
add_action( 'woocommerce_before_shop_loop_item', 'woocommerce_product_loop_tags', 5 );
function woocommerce_product_loop_tags() {
global $post, $product;
if ( is_array (get_terms( 'product_tag' ))) {
$tags = get_terms( 'product_tag' );
echo '<span class="tag-cloud">';
foreach($tags as $tag) {
echo '<span rel="tag" class="tag-'.$tag->slug.'">'.$tag->name.'</span>';
}
echo '</span>';
}
}

How to add path in wp_get_archives()?

I can not find where to change path for wp_get_archives()...Now i have path http://example.com/2015 but i want to add history before 2015 so that i have url:
http//example.com/history/2015. Any suggestion?
<?php wp_get_archives('type=yearly'); ?>
add_filter( 'get_archives_link', function( $html )
{
if( is_admin() ) // Just in case, don't run on admin side
return $html;
// $html is '<li><a href='http://example.com/hello-world'>Hello world!</a></li>'
$html = str_replace( 'href="http://example.com', 'href="http://example.org', $html );
return $html;
});
I found this but when nothing happend when i added

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

Resources