0

I got a strange problem on a Multisite Install I run.

Everything works perfectly fine (Domain Mapping, Caching, whatever functions and Plugins I use, both forced and chooseable).

To enhance the experience for my customers, I installed a simple autoexpiry plugin that I wrote myself quite a while ago. It is not the best solution of entering a date, but apart from the UI it just works.

In my plugin I include two files:

  • cronjobs.php (containing the function for the one-off event to set the post to draft)
  • metaboxes.php (containing the custom metabox for the post, as well as the save-function, and the hooks to use them).

Now the strange part: In WordPress-Admin everything works just as expected. The scheduling of the events to set a post to draft, and the metabox.

The Theme for my Multisite contains a css.php, rendering the custom CSS from the Theme options. If the Plugin is activated, only this file gets a 404.

I already built a workaround, including the file for the metaboxes just in admin. Now the css.php works. My main plugin file looks like this now:

include('inc/cronjobs.php');
if ( is_admin() ) { // I just include the metaboxes.php in the admin section now
    include('inc/metaboxes.php');
}

Without the conditional for the include of the metaboxes file, the debug throws this Error:

PHP Fatal error:  Call to undefined function wp_get_current_user() in /www/htdocs/w009bb74/landingpages/wp-includes/capabilities.php on line 1342

Has anyone had a similar problem? I can't figure out what's the problem here. On singlesite installs there is no problem whatsoever. The css.php in the theme does not even work if the metaboxes.php is completely empty

Here are the two files:

cronjobs.php

function f711_autoexpiry_set_draft( $post_id ) {
    $post = array(
        'ID' => $post_id,
        'post_status' => 'draft'
    );
    wp_update_post( $post );
}
add_action( 'f711_autoexpiry_event', 'f711_autoexpiry_set_draft', 10, 1 );

metaboxes.php

add_action( 'add_meta_boxes', 'f711_autoexpiry_expiry_add_custom_box' );
add_action( 'save_post', 'f711_autoexpiry_expiry_save_expirydata' );

function f711_autoexpiry_expiry_add_custom_box() {
    add_meta_box( 
        'f711_autoexpiry_expiry_sectionid',
        __( 'Ablaufdatum', 'f711' ),
        'f711_autoexpiry_expiry_inner_custom_box',
        'angebote' ,
        'side',
        'high'
    );
}

function f711_autoexpiry_expiry_inner_custom_box( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), 'f711_autoexpiry_expiry_nonce' );

  $expirydate = get_post_meta( $post->ID, '_f711_autoexpiry_expirydate', true );

  if ( get_post_meta( $post->ID, '_f711_autoexpiry_expirydate', true ) != "" ) {
    $expirydate = strtotime( get_post_meta( $post->ID, '_f711_autoexpiry_expirydate', true ) );
    $expiryday = date( 'd', $expirydate );
    $expirymonth = date( 'm', $expirydate );
    $expiryyear = date( 'Y', $expirydate );
    $expiryhour = date( 'H', $expirydate );
    $expiryminute = date( 'i', $expirydate );
  }

  $styles = 'style="width: 40px;margin-right:8px;"';
  $styleslabel = 'style="width: 40px;margin-right:10px;float:left;"';
  // The actual fields for data entry
  echo '<label for="f711_autoexpiry_expiry_tag" ' . $styleslabel . '>';
       _e("Tag", 'f711_du' );
  echo '</label> ';
  echo '<label for="f711_autoexpiry_expiry_monat"' . $styleslabel . '>';
       _e("Mon.", 'f711_du' );
  echo '</label> ';
  echo '<label for="f711_autoexpiry_expiry_jahr"' . $styleslabel . '>';
       _e("Jahr", 'f711_du' );
  echo '</label> ';
  echo '<label for="f711_autoexpiry_expiry_stunde"' . $styleslabel . '>';
       _e("Stu.", 'f711_du' );
  echo '</label> ';
  echo '<label for="f711_autoexpiry_expiry_minute"' . $styleslabel . '>';
       _e("Min.", 'f711_du' );
  echo '</label><br />';
  echo '<input type="text" id="f711_autoexpiry_expiry_tag" name="f711_autoexpiry_expiry_tag" value="' . $expiryday . '" size="2" ' . $styles . ' />';
  echo '<input type="text" id="f711_autoexpiry_expiry_monat" name="f711_autoexpiry_expiry_monat" value="' . $expirymonth . '" size="2" ' . $styles . ' />';
  echo '<input type="text" id="f711_autoexpiry_expiry_jahr" name="f711_autoexpiry_expiry_jahr" value="' . $expiryyear . '" size="2" ' . $styles . ' />';
  echo '<input type="text" id="f711_autoexpiry_expiry_stunde" name="f711_autoexpiry_expiry_stunde" value="' . $expiryhour . '" size="2" ' . $styles . ' />';
  echo '<input type="text" id="f711_autoexpiry_expiry_minute" name="f711_autoexpiry_expiry_minute" value="' . $expiryminute . '" size="2" ' . $styles . ' />';
  echo '<br /><br />';
  echo '<input type="checkbox" id="f711_autoexpiry_clear_expiry" name="f711_autoexpiry_clear_expiry" value="clear"> Ablaufdatum löschen';
  echo '<br /><br />';
  echo '<p>Format: z.B. 31 03 2014 07 00</p>';
}

/* When the post is saved, saves our custom data */
function f711_autoexpiry_expiry_save_expirydata( $post_id ) {

  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;

  if ( !wp_verify_nonce( $_POST['f711_autoexpiry_expiry_nonce'], plugin_basename( __FILE__ ) ) )
      return;

  if ( $_POST['f711_autoexpiry_clear_expiry'] == 'clear' || $_POST['f711_autoexpiry_expiry_tag'] == "" ) {
      delete_post_meta( $post_id, '_f711_autoexpiry_expirydate' );
      wp_clear_scheduled_hook( 'f711_autoexpiry_event', array( $post_id ) );
      return;
  }

  $tag = $_POST['f711_autoexpiry_expiry_tag'];
  $monat = $_POST['f711_autoexpiry_expiry_monat'];
  $jahr = $_POST['f711_autoexpiry_expiry_jahr'];
  $stunde = $_POST['f711_autoexpiry_expiry_stunde'];
  $minute = $_POST['f711_autoexpiry_expiry_minute'];

  if( substr( $tag, 0, 1 ) != "0" && $tag < 10) $tag = "0" . $tag;
  if( substr( $monat, 0, 1 ) != "0" && $monat < 10) $monat = "0" . $monat;
  if( substr( $stunde, 0, 1 ) != "0" && $stunde < 10) $stunde = "0" . $stunde;
  if( substr( $minute, 0, 1 ) != "0" && $minute < 10) $minute = "0" . $minute;
  $usedate = $jahr . '-' . $monat . '-' . $tag . ' ' . $stunde . ':' . $minute . ':00';
  if ( $jahr > 2000 && $tag > "00" && $monat > "00" && $usedate != "1970-01-01" ) {
      update_post_meta( $post_id, '_f711_autoexpiry_expirydate', $usedate );
  }
  $offset = '-' . get_option('gmt_offset') . ' hour';
  $thisdate = strtotime( $offset , strtotime( $usedate ) );
    wp_clear_scheduled_hook( 'f711_autoexpiry_event', array( $post_id ) );
    wp_schedule_single_event( $thisdate, 'f711_autoexpiry_event', array( $post_id ) );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.