I want to have one page with the list with titles of posts. And every title should link to external page (using custom field named 'link'). I have this code, but it changes the link everywhere on the site and I want it to work only on a specific page, let's say on a page named "Example". On a homepage and everywhere except the page "Example" it should link to the post.
add_filter( 'post_link', 'links', 10, 2 );
function links( $link, $post )
{
$meta = get_post_meta( $post->ID, 'link', TRUE );
$url = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );
return $url ? $url : $link;
}
You can check for queried_object on global $wp_query;
add_filter( 'post_link', 'links', 10, 2 );
function links( $link, $post )
{
$meta = get_post_meta( $post->ID, 'link', TRUE );
$url = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );
if($wp_query->queried_object instanceof WP_Post && $wp_query->queried_object->ID == 258){
return $url ? $url : $link;
}else{
return $link;
}
}
Related
function add_custom_fields($post_id) {
global $post;
$metadescription = wp_trim_words( get_the_content(), 55 );
add_post_meta( $post_id, 'meta_description', $metadescription, true );
}
add_action( 'wp_insert_post', 'add_custom_fields' );
what i going wrong?
Below is a code that will add_post_meta every time a post or page is created or updated on your website.
function add_custom_fields($post_id) {
// If this is a revision, don't add content.
if ( wp_is_post_revision( $post_id ) )
return;
global $post;
$metadescription = wp_trim_words( apply_filters('the_content', $post->post_content), 55, '');
add_post_meta($post_id, 'meta_description',$metadescription, true);
}
add_action( 'wp_insert_post', 'add_custom_fields', 10, 1 );
Let say a post have parent-category 'Recipes', child-category 'Baking' and post name 'My post title'
Apparently, the post can be accessible via 2 link:
https://example.com/recipes/baking/my-post-title/
https://example.com/recipes/my-post-title/
I just want the post to be accessible via the first link, not the latter. Is it possible?
Many thanks!
FYI, I used /%category%/%postname%/ as custom structure for permalinks
you can use both post_link hook and a custom rewrite rule.
custom rewrite function:
function post_add_rewrite_rule(){
$posts = get_posts( array( 'numberposts' => -1) );
if( $posts ){
foreach($posts as $post ){
$post_terms = wp_get_object_terms( $post->ID, 'recipes' );
if( $post_terms ){
add_rewrite_rule( "^recipes/" . array_pop($post_terms)->slug . "/([^/]+)/?$", 'index.php?post_type=post&name=$matches[1]', 'top' );
}
}
}
}
add_action('init', 'post_add_rewrite_rule');
post link hook:
function post_custom_permalink( $url, $post, $leavename=false ) {
if ( $post->post_type == 'post' ) {
$post_terms = wp_get_object_terms( $post->ID, 'recipes' );
if( !empty($post_terms) ){
$url = home_url( '/recipes/' . array_pop($post_terms)->slug . '/' . $post->post_name );
}
else{
$url = home_url( '/recipes/' . $post->post_name );
}
}
return $url;
}
add_filter( 'post_link', 'post_custom_permalink', 10, 3 );
and finally set up your permalink structure to custom structure:
/recipes/%postname%/
I want a function that triggers when a new WordPress post publish.
add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_notification( $post_id, $post ) {
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$permalink = get_permalink( $post_id );
$edit = get_edit_post_link( $post_id, '' );
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$message = sprintf( 'Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
$message .= sprintf( 'View: %s', $permalink );
$headers[] = '';
wp_mail( $to, $subject, $message, $headers );
}
I tried this one. I tried also different statuses such as save_post but I didn't get any email when the user add a new post and publish it. so it doesn't work. how can I solve this problem?
You use the publish_post hook. If get_post_meta() returns a value, it will trigger your actions to occur.
function run_on_post_publish( $post_id ) {
$post_data = get_post_meta( $post_id, 'run_once', true );
if ( ! empty( $post_data ) ) {
return;
}
// Run the action you wish to occur when a post is published
update_post_meta( $post_id, 'run_once', true );
}
add_action( 'publish_post', 'run_on_post_publish' );
I have using this function code to make new Custom Text Area, but have issue to show it in product page.
// Custom Field Product
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
add_action( 'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Custom Text:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save Changes to DB
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea
) );
}
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
Looks like when press save, its storing data in DB, but its not showing in single product page. Can someone to tell me where is issue in this function?
The problem is that in the following function, $post is not defined. (The code was indented for clarity.)
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
So, a simple fix would be to add global $post; to the function:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $post;
// custom content.
if ( $post ) {
echo get_post_meta( $post->ID, '_textarea', true );
}
}
Alternatively, you can use the global $product object:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $product;
// custom content.
if ( $product ) {
echo get_post_meta( $product->get_id(), '_textarea', true );
}
}
I search all the web for that answer. I use wp_list_categories to make a submenu with custom taxonomy, It works well, and puts current-cat when I browse those categories.
The thing is, when I browse single posts with this menu, the highlight no more works.
For the blog part of that site, I use the following code to highlight current category on wp_list_categories():
function sgr_show_current_cat_on_single($output) {
global $post;
if( is_single() ) {
$categories = wp_get_post_categories($post->ID);
foreach( $categories as $catid ) {
$cat = get_category($catid);
if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
}
}
}
return $output;
}
add_filter('wp_list_categories', 'sgr_show_current_cat_on_single');
But as far as I tried, can't make it work for single posts that are ordered by custom taxonomy. :/ > I don't know how to custom it.
Is it even possible ?
You need to use get_the_terms( $id, $taxonomy ); instead of wp_get_post_categories(); for getting custom taxonomy term IDs.
You can hardcode taxonomy name into the functon or get it from the $args you passed into wp_list_categories( $args );.
Final code:
add_filter( 'wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2 );
function sgr_show_current_cat_on_single( $output, $args ) {
if ( is_single() ) :
global $post;
$terms = get_the_terms( $post->ID, $args['taxonomy'] );
foreach( $terms as $term ) {
if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) ) {
$output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
}
}
endif;
return $output;
}