vendredi 18 décembre 2015

PHP str_ireplace in bulk landing page creator

Im trying to modify the old bulk landing page creator to separate separate the city and state fields. Problem is it doesnt look the like the str_ireplace is not working working correctly. When i enter in the list of city names, and the state i can get the city name to appear in the page title, content and permalink but the state value is not appearing in the page title, content and permalinks.

Below is the whole code. Can someone help me with this

<?php

if(is_admin())
{
    global $table_prefix,$wpdb;
    add_action('admin_menu','auto_generate_plugin_admin_menu');

    function auto_generate_plugin_admin_menu() // Functions to be called in wp admin
    {

        add_options_page('Manage Contents','LNB Bulk Service Area Pages','administrator','content_settings','LNB_settings_page');
    }

}

function LNB_settings_page(){

    global $table_prefix,$wpdb;

    if(isset($_REQUEST['action']))
    {
        $cityname_arr = explode("\n",$_REQUEST['cityname']);
    $statename = explode("\n",$_REQUEST['statename']);
        $title=$_REQUEST['page_title'];
        foreach($cityname_arr as $cityname){
            $cityname = str_replace(array("\n","\r","\r\n","\n\r")," ",$cityname);                          

            $array_from = array ('[lnb-city]', '[lnb-state]'); 
            $array_to = array ($cityname, $statename);

            $title2 = str_ireplace ($array_from, $array_to, $title);            

            //$title2= str_ireplace("[lnb-city]",$cityname,$title);
            $page_contents = str_ireplace("[lnb-city]",$cityname,str_replace(array("\n","\r","\r\n","\n\r")," ",$_REQUEST['page_contents']));
            $post_array=array(
              'post_content'   => $page_contents,
              'post_status'    => 'publish',
              'post_title'     => $title2,
              'post_type'      => 'page',
              'post_name'      => str_replace(",","-",$title2) //post slug
            ); // Post Array End
            if(isset($_REQUEST['parent_page']) && $_REQUEST['parent_page']>=1)
                $post_array['post_parent']=$_REQUEST['parent_page'];


            $post = $post_array;
            $id = wp_insert_post($post);
            update_post_meta($id,'lnb-city',$title2);
            if(isset($_REQUEST['page_template']) && $_REQUEST['page_template']!='')
            update_post_meta($id, '_wp_page_template', $_REQUEST['page_template']);
            $update_message = '<div id="message" class="updated">
 <p>
   Pages Created. Click here to <a href="edit.php?post_status=draft&post_type=page">View Pages</a>
 </p>
</div>';

        }//End Foreach cityname_arr
    }//END IF 'Action'
    else{
                    $cityname='';
                                $title2='';
                    $imagename='';
                    $extra_contents='';
                    $update_message='';
    }//End Else

    ?>

    <div id="lnb-service-area-form">
            <?php echo $update_message; ?>
        <h2><?php echo _e('Auto Generated Page Contents Options');?></h2>
        <form method="post" action="" onsubmit="return validate_submit()">
        <?php wp_nonce_field('update-options'); ?>
        <table>
        <tr>
            <td valign="top"><?php echo _e('City Text Field');?></td>
            <td>
                <textarea id="cityname" type="text" cols="40" rows="5" name="cityname" placeholder="Ex: Raleigh, NC"></textarea>
                                <br><label><?php _e("Please list one City,State per line"); ?></label>
            </td>
        </tr>
    <tr>
            <td valign="top"><?php echo _e('State Field');?></td>
            <td>
                <input id="statename" type="text" cols="40" rows="5" name="statename" placeholder="Ex:NC" />
                                <br><label><?php _e("Please enter the state"); ?></label>
            </td>
        </tr>  
        <tr>
            <td valign="top"><?php echo _e('Parent Page');?></td>
            <td><?php
                                  $pages = get_pages(); // Gets all pages type posts
                                  if($pages && !empty($pages)){
                                      echo "<select name='parent_page' id='parent_page' ><option value='0'>None</option>";
                                      foreach ( $pages as $page ) {
                                            $option = '<option value="' . $page->ID . '">';
                                            $option .= $page->post_title;
                                            $option .= '</option>';
                                            echo $option;
                                      }
                                      echo "</splect>";
                                  }// End if Pages
                              ?>
                        </td>
        </tr>
        <tr>
        <td valign="top"><?php echo _e('Page Template');?></td><td>

        <?php
                                  $templates = get_page_templates(); //Get all available templates in the current themes
                                  if($templates && !empty($templates)){
                                      echo "<select name='page_template' id='page_template' ><option value=''>Default</option>";
                                      foreach ( $templates as $template_name => $template_filename ) {
                                            $option = '<option value="' . $template_filename  . '">';
                                            $option .= $template_name;
                                            $option .= '</option>';
                                            echo $option;
                                      }
                                      echo "</splect>";
                                  } //End IF Templates
                              ?>
        </td>
        </tr>
                <tr>
        <td valign="top"><?php echo _e('Page Title');?></td><td>
                    <input placeholder="<?php echo _e('ex: Find Web Design in [lnb-city]');?>" type="text" name="page_title" id="page_title" size="70">
        </td>
        </tr>
                <tr>
        <td valign="top" ><?php echo _e('Page Contents');?></td><td>
                    <textarea rows="15" cols="120"name="page_contents" id="page_contents"></textarea><br />
                    <?php echo _e('HTML Markup accepted as well as the [lnb-city] shortcode. Shortcode will be transformed into your city name.'); ?>
        </td>
        </tr>
        </table>
        <input type="hidden" name="action" value="update" >
        <p>
        <input type="submit" value="<?php echo _e('Create Service Area Pages'); ?>">
        </p>
        </form>
    </div>
<script language="javascript">
    function validate_submit() // Form values validation function
    {
        msg='';
        if(document.getElementById('upload_image').value == '')
        {
            msg = msg+"Please enter any image URL or upload an image\n";
        }
        if(document.getElementById('cityname').value == '')
        {
            msg = msg+"Please enter City Name for the image\n";
        }
        if(document.getElementById('extra_contents').value == '')
        {
            msg = msg+"Please enter some extra contents \n";
        }
        if(msg=='')
            {
                return true
            } //End If msg
            else
                {
                    alert(msg); // Message will be displayed to the user if there is any invalid values
                    return false;
                } //end else msg
    }// Validation FUnction Ends
    </script>
<?php
}// End Function LNB_settings_page
?>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire