0

I have a custom plugin, that creates an admin settings page, with 3 fields:

postIDs, pageIDs and Message.

I am trying to grab the first post displayed on the homepage list and add the contents of the field "message" to the beginning of the content. I'm doing this within a plugin, and I can't work out how to hook into the homepage posts list, get the first post there, and add the "Message" before the content then return it back.

I figured it was something to do with pre_get_posts hook - but I just can't work out the correct way to do it.

Here's the full PHP of the plugin, it all works, I just can't now work out the correct way to get the first post content :(

<?php
/*
Plugin Name: Custom HTML
Description: Custom HTML in Pages
Version: 1.0.0
*/
class Codeable_Fields_Plugin {
  public function __construct() {
    // Hook into the admin menu
    add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
    // Add Settings and Fields
    add_action( 'admin_init', array( $this, 'setup_sections' ) );
    add_action( 'admin_init', array( $this, 'setup_fields' ) );
  }
  public function create_plugin_settings_page() {
    // Add the menu item and page
    $page_title = 'Custom HTML';
    $menu_title = 'Custom HTML';
    $capability = 'manage_options';
    $slug = 'codeable_fields';
    $callback = array( $this, 'plugin_settings_page_content' );
    $position = 100;
    add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
  }
  public function plugin_settings_page_content() {?>
    <div class="wrap">
      <?php
      if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){
        $this->admin_notice();
      } ?>
      <form method="POST" action="options.php">
      <?php
        settings_fields( 'codeable_fields' );
        do_settings_sections( 'codeable_fields' );
        submit_button();
      ?>
      </form>
    </div> <?php
  }
  public function admin_notice() { ?>
    <div class="notice notice-success is-dismissible">
      <p>Your settings have been updated!</p>
    </div><?php
  }
  public function setup_sections() {
    add_settings_section( 'our_first_section', 'Custom HTML', array( $this, 'section_callback' ), 'codeable_fields' );
  }
  public function section_callback( $arguments ) {
    switch( $arguments['id'] ){
      case 'our_first_section':
        echo '';
        break;
      }
  }
  public function setup_fields() {
    $fields = array(
      array(
        'uid' => 'codeable_pages_field',
        'label' => 'Page IDs to Exclude',
        'section' => 'our_first_section',
        'type' => 'text',
        'supplimental' => 'Seperate by comma',
      ),
      array(
        'uid' => 'codeable_posts_field',
        'label' => 'Post IDs to Exclude',
        'section' => 'our_first_section',
        'type' => 'text',
        'supplimental' => 'Seperate by comma',
      ),
      array(
        'uid' => 'codeable_textarea',
        'label' => 'Enter Text here',
        'section' => 'our_first_section',
        'type' => 'textarea',
      )
    );
    foreach( $fields as $field ){
      add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), 'codeable_fields', $field['section'], $field );
      register_setting( 'codeable_fields', $field['uid'] );
    }
  }
  public function field_callback( $arguments ) {
    $value = get_option( $arguments['uid'] );
    if( ! $value ) {
      $value = $arguments['default'];
    }
    switch( $arguments['type'] ){
      case 'text':
      case 'password':
      case 'number':
        printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />', $arguments['uid'], $arguments['type'], $arguments['placeholder'], $value );
        break;
      case 'textarea':
        printf( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>', $arguments['uid'], $arguments['placeholder'], $value );
        break;
      case 'select':
      case 'multiselect':
        if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ){
          $attributes = '';
          $options_markup = '';
          foreach( $arguments['options'] as $key => $label ){
            $options_markup .= sprintf( '<option value="%s" %s>%s</option>', $key, selected( $value[ array_search( $key, $value, true ) ], $key, false ), $label );
          }
          if( $arguments['type'] === 'multiselect' ){
            $attributes = ' multiple="multiple" ';
          }
          printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>', $arguments['uid'], $attributes, $options_markup );
        }
        break;
      case 'radio':
      case 'checkbox':
        if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ){
          $options_markup = '';
          $iterator = 0;
          foreach( $arguments['options'] as $key => $label ){
            $iterator++;
            $options_markup .= sprintf( '<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>', $arguments['uid'], $arguments['type'], $key, checked( $value[ array_search( $key, $value, true ) ], $key, false ), $label, $iterator );
          }
          printf( '<fieldset>%s</fieldset>', $options_markup );
        }
        break;
      }
      if( $helper = $arguments['helper'] ){
        printf( '<span class="helper"> %s</span>', $helper );
      }
      if( $supplimental = $arguments['supplimental'] ){
        printf( '<p class="description">%s</p>', $supplimental );
      }
    }
  }
}
new Codeable_Fields_Plugin();

add_action ('the_content', 'add_to_header');
function add_to_header(){
  $postids = preg_split("/\s*,\s*/", get_option('codeable_posts_field'));
  $pageids = preg_split("/\s*,\s*/", get_option('codeable_pages_field'));

  if (is_single($postids )) {
    $fullcontent = $content;
  }
  elseif (is_page($pageids)) {
    $fullcontent = $content;
  }
  elseif (is_single() || is_page()) {
    $beforecontent = get_option('codeable_textarea');
    $fullcontent = $beforecontent . $content;
  }
  return $fullcontent;
}

Any help would be gratefully received!!

2 Answers 2

0

You can use the loop_start hook which passes the $WP_Query object by reference.

namespace StackExchange\WordPress;
class editFirstPostContent {
  public function load() {
    if( \is_admin() ) {
      return;
    }
    \add_filter( 'loop_start', [ $this, 'loopStart' ] );
  }
  public function loopStart( \WP_Query $WP_Query) {
    if( ! $WP_Query->is_main_query() ) {
      return;
    }
    if( ! isset( $WP_Query->posts[0] ) ) {
      return;
    }
    $WP_Query->posts[0]->post_content = sprintf(
      '<div>Hello World!</div>%1$s',
      $WP_Query->posts[0]->post_content
    );
  }
}
$editFirstPostContent = new editFirstPostContent();
\add_action( 'plugins_loaded', [ $editFirstPostContent, 'load' ] );
0

For reference, I solved it with this code:

function is_latest() {
    global $post;
    $loop = get_posts( 'numberposts=1' );
    $latest = $loop[0]->ID; 
    return ( $post->ID == $latest ) ? true : false;
}   

Then called the is_latest() function into the if statement at the end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.