Snippets

Willy Bahuaud Améliorer la fonctionnalité des « contenus à la une » de WordPress / Improve WordPress “Sticky Posts” feature

Updated by Willy Bahuaud

File sync-sticky-values-with-acf.php Modified

  • Ignore whitespace
  • Hide word diff
     return $check;
 }
 
-add_filter( 'updated_post_meta', 'w_update_sticky_post_meta', 10, 4 );
+add_action( 'updated_post_meta', 'w_update_sticky_post_meta', 10, 4 );
 function w_update_sticky_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
     if ( get_option( 'page_for_posts' ) == $object_id
       && 'sticky_posts' === $meta_key ) {
Updated by Willy Bahuaud

File sync-sticky-values-with-acf.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+add_filter( 'pre_option_options_sticky_posts', 'w_get_sticky' );
+function w_get_sticky() {
+    return get_option( 'sticky_posts' );
+}
+
+add_action( 'update_option', 'w_update_sticky', 10, 3 );
+function w_update_sticky( $option, $old_value, $value ) {
+    if ( 'options_sticky_posts' === $option ) {
+        update_option( 'sticky_posts', $value );
+    }
+}
+
+add_filter( 'get_post_metadata', 'w_get_sticky_post_meta', 10, 3 );
+function w_get_sticky_post_meta( $check, $object_id, $meta_key ) {
+    if ( get_option( 'page_for_posts' ) == $object_id
+      && 'sticky_posts' === $meta_key ) {
+        return array( get_option( 'sticky_posts' ) );
+    }
+    return $check;
+}
+
+add_filter( 'updated_post_meta', 'w_update_sticky_post_meta', 10, 4 );
+function w_update_sticky_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ) {
+    if ( get_option( 'page_for_posts' ) == $object_id
+      && 'sticky_posts' === $meta_key ) {
+        update_option( 'sticky_posts', $_meta_value );
+    }
+}
Updated by Willy Bahuaud

File sticky-cpt.php Modified

  • Ignore whitespace
  • Hide word diff
 
 function need_sticky( $post ) {
     $post_type = $post->post_type;
-	$post_type_object = get_post_type_object( $post_type );
-	$can_publish = current_user_can( $post_type_object->cap->publish_posts );
+    $post_type_object = get_post_type_object( $post_type );
+    $can_publish = current_user_can( $post_type_object->cap->publish_posts );
     return $can_publish && in_array( $post->post_type, array( 'page', 'event' ) );
 }
 
Updated by Willy Bahuaud

File sticky-cpt.php Modified

  • Ignore whitespace
  • Hide word diff
 <?php
 
 function need_sticky( $post ) {
-    return in_array( $post->post_type, array( 'page', 'event' ) );
+    $post_type = $post->post_type;
+	$post_type_object = get_post_type_object( $post_type );
+	$can_publish = current_user_can( $post_type_object->cap->publish_posts );
+    return $can_publish && in_array( $post->post_type, array( 'page', 'event' ) );
 }
 
 add_action( 'post_submitbox_minor_actions', 'w_post_submitbox_minor_actions' );
     if ( need_sticky( $post ) ) {
         $temp = ob_get_clean();
         
-        $sticky_box = '<input type="checkbox" style="display:none" name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" '
+        $sticky_box = '<input type="checkbox" style="display:none" 
+            name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" '
             . checked( is_sticky( $post->ID ), true, false ) . ' />';
 
-        $sticky_box .= sprintf( '<span id="sticky-span"><input id="super-sticky" name="sticky" type="checkbox" value="sticky" %1$s />
-        <label for="super-sticky" class="selectit">%2$s</label><br/></span>',
-        checked( is_sticky( $post->ID ), true, false ),
-        __( 'Stick this post to the front page' ) );
+        if ( current_user_can( 'edit_others_posts' ) ) {
+            $sticky_box .= sprintf( '<span id="sticky-span"><input id="super-sticky" 
+                name="sticky" type="checkbox" value="sticky" %1$s />
+                <label for="super-sticky" class="selectit">%2$s</label><br/></span>',
+                checked( is_sticky( $post->ID ), true, false ),
+                __( 'Stick this post to the front page' ) );
+        }
 
         $re = '/(<input.*id="visibility-radio-password")/U';
         $output = preg_replace( $re, "{$sticky_box}$1", $temp );
Updated by Willy Bahuaud

File sticky-cpt.php Modified

  • Ignore whitespace
  • Hide word diff
 <?php
 
+function need_sticky( $post ) {
+    return in_array( $post->post_type, array( 'page', 'event' ) );
+}
+
 add_action( 'post_submitbox_minor_actions', 'w_post_submitbox_minor_actions' );
 function w_post_submitbox_minor_actions( $post ) {
-    ob_start();
+    if ( need_sticky( $post ) ) {
+        ob_start();
+    }
 }
 
 add_action( 'post_submitbox_misc_actions', 'w_post_submitbox_misc_actions' );
 function w_post_submitbox_misc_actions( $post ) {
-    $temp = ob_get_clean();
-    
-    $hidden_sticky = '<input type="checkbox" style="display:none" name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" '
-        . checked( is_sticky( $post->ID ), true, false ) . ' />';
+    if ( need_sticky( $post ) ) {
+        $temp = ob_get_clean();
+        
+        $sticky_box = '<input type="checkbox" style="display:none" name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" '
+            . checked( is_sticky( $post->ID ), true, false ) . ' />';
 
-    $sticky_box = sprintf( '<span id="sticky-span"><input id="super-sticky" name="sticky" type="checkbox" value="sticky" %1$s />
-    <label for="super-sticky" class="selectit">%2$s</label><br/></span>',
-    checked( is_sticky( $post->ID ), true, false ),
-    __( 'Stick this post to the front page' ) );
+        $sticky_box .= sprintf( '<span id="sticky-span"><input id="super-sticky" name="sticky" type="checkbox" value="sticky" %1$s />
+        <label for="super-sticky" class="selectit">%2$s</label><br/></span>',
+        checked( is_sticky( $post->ID ), true, false ),
+        __( 'Stick this post to the front page' ) );
 
-    $re = '/(<input.*id="visibility-radio-password")/U';
-    $output = preg_replace( $re, "{$mb}$1", $temp );
-    echo $output;
+        $re = '/(<input.*id="visibility-radio-password")/U';
+        $output = preg_replace( $re, "{$sticky_box}$1", $temp );
+        echo $output;
+    }
 }
  1. 1
  2. 2
  3. 3
  4. 4
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.