Jump to content

PHP help editing latest news functionality


TimberJon

Recommended Posts

Hello all,

 

My question is about a custom-coded latest-news plugin written by someone else. I don't know if it is generic, a copy of something else or what. It works good! but when I go to add a new news item to the list under Settings, it adds the item to the bottom of the list. The news is displayed on my homepage in short form and in full format on the primary news page.

 

I need new items to be added to the TOP of the list instead or in reverse, so that in all the containers where it resides, the latest news item added is always at the top of the list. I know nothing about php but I am looking to learn. I plan on taking some basic classes on css and php to upgrade my archaic html skills, but I'm currently studying other subjects. Regardless, if this has a simple fix, I would be very interested in knowing what is needed and where/why.

 

Here is the core of the php that I am finding in the plugin: There are three php files, This one says active, the other two say inactive so I get the impression they are not in use. Let me know if I should post their contents as well.

 

Appreciate any help with this! My sincere thanks in advance.

 

Labeled active: latest_news.php

*/

ob_start();

register_activation_hook(__file__, latest_news_install);

 

function latest_news_install() {

 

    global $wpdb;

 

    $table_name = $wpdb->prefix . "latest_news";

 

    if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {

 

        $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (

                    `id` int(10) NOT NULL AUTO_INCREMENT,

                    `title` varchar(255) NOT NULL,

                    `description` text NOT NULL,

                    `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

                    PRIMARY KEY (id)

                );";

 

        mysql_query($sql) or die(mysql_error());

    }

}

 

if( !function_exists('wp_latest_news_init') ) {

 

    function wp_latest_news_init() {

 

        add_shortcode( 'wp_latest_news', 'shortcode_latest_news');

    }

}

if( !function_exists('shortcode_latest_news') ) {

 

    function shortcode_latest_news() {

 

      // extract(shortcode_atts(array('limit' => '5', ), $atts));

 

        global $wpdb;     

 

        $user_count = $wpdb->get_results("SELECT * FROM wp_latest_news ORDER BY date ASC LIMIT 4");

       

        foreach ($user_count as $output) {

            $htmlOutput[] = "<li><b><a href='".  get_permalink(287)."?news_id=".$output->id."'>" . $output->title . "</a></b></li>";

 

        }

        //echo $htmlOutput;

        //echo "<pre>"; print_r($user_count); echo "</pre>";

        foreach ($htmlOutput as $out) {

            echo $out;

        }

       

        return $htmlOutput;

 

    }

}

 

 

/* add_action('init', 'wp_latest_news_init' ) is used for call init function for create shortcode */

add_action('init', 'wp_latest_news_init' );

add_action('admin_menu', 'wp_latestnews_links' );

 

 

function wp_latestnews_links() {

 

    add_submenu_page( 'options-general.php', __( 'Latest News ' ), __( 'Latest News ' ), 5 , 'latest_news_settings', 'latest_news_settings' );

 

}

 

//    function latest_news_settings() {

//          global $wpdb;

//        if (isset($_POST["submit"])) {

//            $sql="INSERT INTO ".$wpdb->prefix . "latest_news(`title`,`description`) VALUES ('".$_POST['news_title']."','".$_POST['description']."') ";

//          //$sql=$wpdb->insert( $wpdb->latest_news, array( 'title' => $_POST['news_title'] ,'description' => $_POST['description'] ) );

//          mysql_query($sql) or die(mysql_error());

//        }

//        include 'Latest_news_setting.php';

 

 

function latest_news_settings() {

    global $wpdb;

    $action = $_REQUEST["action"];

    $id = $_REQUEST['id'];

 

    $sql = "SELECT * from ".$wpdb->prefix."latest_news ";

    $results = $wpdb->get_results($sql);

    //echo "<pre>";print_r($results);

    //$post_arr = query_posts("");

    //echo "<pre>";print_r($post_arr);exit;

    switch ($action) {

        case 'add' :

            include('news_form.php');

            break;

        case 'save' :

            if(isset($_POST['cancel'])) {

                include('latest_news_list.php');

            } else if(isset($_POST['add'])) {

 

                $wpdb->query("insert into ".$wpdb->prefix."latest_news (`title`,`description`) VALUES ('".$_REQUEST['news_title']."','".$_REQUEST['description']."')");

 

                //header("Location: http://localhost:81/Bravo/wp-admin/edit.php?page=slider_images");

                header("location:?page=latest_news_settings");

            }

            break;

 

        case 'edit' :

            $sql = "SELECT * from ".$wpdb->prefix."latest_news where id = '".$id."' ";

            $results_arr = $wpdb->get_results($sql);

            //echo "<pre>";print_r($results_arr);

            include('news_form.php');

            break;

 

        case 'update' :

            if(isset($_POST['cancel'])) {

                include('news_form.php');

            } else if(isset($_POST['edit'])) {

                //echo $id;

                $wpdb->query("update ".$wpdb->prefix."latest_news set title = '".$_REQUEST['news_title']."', description = '".$_REQUEST['description']."' where id='".$id."'");

                header("location:?page=latest_news_settings");

            }

            break;

 

        case 'delete' :

 

            $wpdb->query("delete from ".$wpdb->prefix."latest_news where id='".$id."'");

 

            header("location:?page=latest_news_settings");

            break;

 

        default:

            include('latest_news_list.php');

            break;

    }

 

}

?>

Link to comment
Share on other sites

Guess I can't edit my post. Thought the PHP button was used for php code.

 

<?php
/* 
*/
ob_start();
register_activation_hook(__file__, latest_news_install);

function latest_news_install() {

    global $wpdb;

    $table_name = $wpdb->prefix . "latest_news";

    if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {

        $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
                     `id` int(10) NOT NULL AUTO_INCREMENT,
                     `title` varchar(255) NOT NULL,
                     `description` text NOT NULL,
                     `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
                     PRIMARY KEY (id)
                 );";

        mysql_query($sql) or die(mysql_error());
    }
}

if( !function_exists('wp_latest_news_init') ) {

    function wp_latest_news_init() {

        add_shortcode( 'wp_latest_news', 'shortcode_latest_news');
    }
}
if( !function_exists('shortcode_latest_news') ) {

    function shortcode_latest_news() {

       // extract(shortcode_atts(array('limit' => '5', ), $atts));

        global $wpdb;      

        $user_count = $wpdb->get_results("SELECT * FROM wp_latest_news ORDER BY date ASC LIMIT 4");
        
        foreach ($user_count as $output) {
            $htmlOutput[] = "<li><b><a href='".  get_permalink(287)."?news_id=".$output->id."'>" . $output->title . "</a></b></li>";

        }
        //echo $htmlOutput;
        //echo "<pre>"; print_r($user_count); echo "</pre>";
        foreach ($htmlOutput as $out) {
            echo $out;
        }
        
        return $htmlOutput;

    }
}


/* add_action('init', 'wp_latest_news_init' ) is used for call init function for create shortcode */
add_action('init', 'wp_latest_news_init' );
add_action('admin_menu', 'wp_latestnews_links' );


function wp_latestnews_links() {

    add_submenu_page( 'options-general.php', __( 'Latest News ' ), __( 'Latest News ' ), 5 , 'latest_news_settings', 'latest_news_settings' );

}

//     function latest_news_settings() {
//          global $wpdb;
//        if (isset($_POST["submit"])) {
//            $sql="INSERT INTO ".$wpdb->prefix . "latest_news(`title`,`description`) VALUES ('".$_POST['news_title']."','".$_POST['description']."') ";
//           //$sql=$wpdb->insert( $wpdb->latest_news, array( 'title' => $_POST['news_title'] ,'description' => $_POST['description'] ) );
//           mysql_query($sql) or die(mysql_error());
//         }
//         include 'Latest_news_setting.php';


function latest_news_settings() {
    global $wpdb;
    $action = $_REQUEST["action"];
    $id = $_REQUEST['id'];

    $sql = "SELECT * from ".$wpdb->prefix."latest_news ";
    $results = $wpdb->get_results($sql);
    //echo "<pre>";print_r($results);
    //$post_arr = query_posts("");
    //echo "<pre>";print_r($post_arr);exit;
    switch ($action) {
        case 'add' :
            include('news_form.php');
            break;
        case 'save' :
            if(isset($_POST['cancel'])) {
                include('latest_news_list.php');
            } else if(isset($_POST['add'])) {

                $wpdb->query("insert into ".$wpdb->prefix."latest_news (`title`,`description`) VALUES ('".$_REQUEST['news_title']."','".$_REQUEST['description']."')");

                //header("Location: http://localhost:81/Bravo/wp-admin/edit.php?page=slider_images");
                header("location:?page=latest_news_settings");
            }
            break;

        case 'edit' :
            $sql = "SELECT * from ".$wpdb->prefix."latest_news where id = '".$id."' ";
            $results_arr = $wpdb->get_results($sql);
            //echo "<pre>";print_r($results_arr);
            include('news_form.php');
            break;

        case 'update' :
            if(isset($_POST['cancel'])) {
                include('news_form.php');
            } else if(isset($_POST['edit'])) {
                //echo $id;
                $wpdb->query("update ".$wpdb->prefix."latest_news set title = '".$_REQUEST['news_title']."', description = '".$_REQUEST['description']."' where id='".$id."'");
                header("location:?page=latest_news_settings");
            }
            break;

        case 'delete' :

            $wpdb->query("delete from ".$wpdb->prefix."latest_news where id='".$id."'");

            header("location:?page=latest_news_settings");
            break;

        default:
            include('latest_news_list.php');
            break;
    }

}
?>

Link to comment
Share on other sites

That works great on the front page! Thank you. It is not displaying the same way on the actual page though. It is listing the news items in the same way with my latest item at the bottom. I'm not sure where else to find that info. It is a custom wordpress site. I have looked inside the plugin itself, page editor where the templates are and this code under the settings area.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.