skip to Main Content

I know this question is probably going to get downvoted and I will probably get into trouble but I am hoping someone may be able to help me with my situation.

On my site I use json to download data from an external source, and then I style it beautifully.

Within the json data is an individual ID for each data set.

What I want to accomplish is to have a database where I can insert the ID and a url link.

I have created the table within the wordpress database via phpMyAdmin, but I want to create a page within the admin section where I can simply add the data in.

For displaying the json data I use a php insert addon, within that php clip i want to do a piece of code that checks the database for the id within my custom database and displays the link.

I will be honest I don’t know where to start on this, even if its just a link to a source that shows me how to create an admin page and submit data to the database within wordpress dashboard.

I really appreciate any help given and like I say I know I should try harder, but when ever I do a search all I get is 100’s of references to add an admin to the database manually.

Thanks,

Adam

Edit I just realized I never put any table information in my question.

The table name within wordpress is called: wp_home_tickets
within that are 3 fields: id (auto increasement), gameid (numeric) and ticketlink (text)

thanks.

2

Answers


  1. For adding a custom settings page in your admin, use the Settings API https://codex.wordpress.org/Settings_API

    Here is a nice tutorial using it https://deliciousbrains.com/create-wordpress-plugin-settings-page/#wp-settings-api

    To fetch data from your custom table, use the wpdb class https://developer.wordpress.org/reference/classes/wpdb/. More specifically, you can use wpdb::get_results if you will have multiple rows sharing the same id https://developer.wordpress.org/reference/classes/wpdb/get_results/

    Or wpdb::get_row if you will ever only have one https://developer.wordpress.org/reference/classes/wpdb/get_row/

    Hope this helps you out!

    Login or Signup to reply.
  2. For anyone wishing to see how it was done, here is how I did it.

    I created a file in my theme called db_admin_menu.php and added the following to it:

    <?php
    function ticket_admin_menu() {
     global $team_page;
     add_menu_page( __( 'Tickets', 'sports-bench' ), __( 'Tickets', 'sports-bench' ), 'edit_posts', 'add_data', 'ticket_page_handler', 'dashicons-groups', 6 ) ;
    }
    add_action( 'admin_menu', 'ticket_admin_menu' );
    
    function ticket_page_handler() {
       $table_name = 'wp_home_tickets';
       global $wpdb;
       echo '<form method="POST" action="?page=add_data">
           <label>Team ID: </label><input type="text" name="gameid" /><br />
           <label>Ticket Link: </label><input type="text" name="ticketlink" /><br />
           <input type="submit" value="submit" />
           </form>';
    
       $default = array(
         'gameid' => '',
         'ticketlink' => '',
       );
      $item = shortcode_atts( $default, $_REQUEST );
      $gameid = $item['gameid'];
      if ($wpdb->get_var("SELECT * FROM wp_home_tickets WHERE gameid = $gameid ")) { echo 'Ticket already exists for this game.'; goto skip; }
      if (!empty($_POST)) { $wpdb->insert( $table_name, $item ); }
      skip:
    }
    ?>
    

    I then put this code in my script that fetches and displays the json:

    $matchid = $match['id'];
    $ticket_url = $wpdb->get_var("SELECT ticketlink FROM wp_home_tickets WHERE gameid = '$matchid' ");
    if ($ticket_url) { echo '<a href="'.$ticket_url.'" class="gamedetail">Get Tickets</a>'; }
    

    I hope someone does find it of use, i did have to use a wordpress plugin called `Insert PHP Code Snippet’ by xyzscripts to be able to snippet the php to a shortcode, but that is not the purpose of this post.

    Thanks again for your help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search