Friday, April 3, 2015

Create Slug and post page object


<?php
function create_slug($string){
   //$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   $slug1=preg_replace('/[^a-z0-9_-]+/i', ' ', trim($string));
   $slug=preg_replace('/[^a-z0-9_-]+/i', '-', trim($slug1));
   echo $slug;
   $ss=explode('-',$slug);
   $slug='';
   if($ss[count($ss)-1]!=''){
  for($i=0;$i<=count($ss)-2;$i++){
  if($ss[$i]!=''){
  $slug.=$ss[$i].'-';
  }
  }
  $slug.=$ss[count($ss)-1];
   }else{
  for($i=0;$i<=count($ss)-3;$i++){
  if($ss[$i]!=''){
  $slug.=$ss[$i].'-';
  }
  }
  $slug.=$ss[count($ss)-2];
   }
   
   echo'<br>'.$slug;
   //return $slug;
}
//returns 'does-this-thing-work-or-not'
function mycreate_page($title){
// Create post object
$my_post = array(
  'post_title'    => $title,
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array(2),
  //'post_parent'   => 23, 
  'post_type'     => 'page'
);

// Insert the post into the database
//echo the_time('Y-m-d H:i:s');
//Y-m-d g:i:s - 2010/11/06 12:50:48 AM 
  wp_insert_post( $my_post );
}
?>


*** Wordpress: Working with wordpress Dashboard

No comments:

Post a Comment