• Dear,

    I just start in wordpress world and I need your help.

    I would like to:

    1. create new blank page
    2. with this new page I need to access to one MySQL table
    3. with the information readed inside MySQL I will show it in the blank page

    The most important is how to access to MySQL in this new page. Maybe a plugin are available to do it ?

    Thanks a lot, Xavi

Viewing 15 replies - 1 through 15 (of 17 total)
  • @xavierator,

    (#1)
    To create a new page in WordPress, you have to create custom page templates.

    (#2 & #3)
    To talk to DB, you can use the $wpdb object, in case you’re querying custom tables. If you’re looking to query standard WordPress tables, you can use the WP_Query class.

    Thread Starter Xavierator

    (@xavierator)

    Dear Maria,

    thanks for your fast answer.

    Some doubts about #1:

    If I understood the better way to create a new custom blank page is copy & rename the page.php in the active theme, maybe something like this ?

    wp-content/themes/twentyseventeen/page.php -> wp-content/themes/twentyseventeen/MyCustomBlankPage.php

    After that, fine, I can execute the url of the new page:

    https://www.tutusaus.cat/wp-content/themes/twentyseventeen/MyCustomBlankPage.php

    Which page content do I need to show as other templates pages ?

    I don’t understand how can I include the new page in WordPress admin pages, on right hand of new pagse in WordPress admin, I only have Parent and Order but not Template options to choose the new one.

    Sorry and thanks again, Xavi

    Moderator bcworkz

    (@bcworkz)

    If I may jump in so you get a quick answer, not only do you rename the copy of page.php, you need to edit the header comment to assign a UI name for your template. For example:

    <?php
    /**
     * Template Name: My Template
     */

    As soon as WP sees there is more than the default page template, a dropdown field allowing template selection will appear on the page edit screen. Pick your template, publish the page, then request the page by its WP permalink. The code on your template file will then execute.

    Thread Starter Xavierator

    (@xavierator)

    Dear,

    OK, I put this lines on my new page and WP recognize it as you told me:

    <?php
    /**
    * Template Name: MyCustomBlankPage
    */?>

    The problem is that the new page (https://www.tutusaus.cat/card-page/) don’t have the default template like other pages (https://www.tutusaus.cat/sample-page/)

    I need to create a new page in WP with de default template and include inside a PHP code to access to MySQL tables.

    Is it possible to do this ?

    Thanks a lot, Xavi

    Moderator bcworkz

    (@bcworkz)

    Your MyCustomBlankPage.php file must contain or reference any and all template code that you want to be part of the page. There’s several ways to do this. For header and footer templates there are functions like get_header() and get_footer() to do this. You can use PHP include and require statements to utilize complete code from other PHP files.

    When you copied page.php, then renamed it, what happened to the PHP code that was on the original file? Is it still on your new code page? Or did you remove it and place your mySQL related code?

    The general idea is to leave all the page.php code on your new page, then simply insert any additional code that you need. Only remove code if you are sure you do not need it. In the case of get_header() and get_footer(), even if you do not desire header and footer template content, you will need to replicate certain required parts like the html and body tags and the head section, as well as calls to wp_head() and wp_footer().

    FYI, when you make edits to your template file, you only need to upload the new version. There’s no need to change the WP page as long as you haven’t changed the template name. The WP page will automatically use the revised file once it’s uploaded. You will need to reload the page in your browser to see the results of your changes though. Sometimes you even need to flush the browser cache.

    Thread Starter Xavierator

    (@xavierator)

    Dear,

    Using get_header() and get_footer() the page works fine:

    https://www.tutusaus.cat/card-page/

    I will try to use $wpdb object and WP_Query class to access to MySQL tables

    If someone have littles samples of MySQL access, please, let me see.

    Thanks, Xavi

    Moderator bcworkz

    (@bcworkz)

    There’s a number of examples at Class Reference/wpdb, but I suspect you’ve seen these. While there are many more specialized methods, the most basic $wpdb->query( $wpdb->prepare(/* sprintf() style query string here */)) is standard mySQL, so your search for examples need not be limited to WP applications. Any mySQL query is feasible.

    Thread Starter Xavierator

    (@xavierator)

    Dear sirs,

    I just resolved the MySQL access from my page using something like:

    global $wpdb;
    $result = $wpdb->get_results( ‘SELECT * FROM wp_usermeta where user_id=’ . get_current_user_id() );
    foreach( $result as $row )
    {
    echo ‘<b>’ . $row->meta_key . ‘</b> = ‘.$row->meta_value . ‘<br/>’;
    }

    Thanks for your help, Xavi

    Thread Starter Xavierator

    (@xavierator)

    Dear,

    I just finish the page with PHP code inside.

    I need to show a “PDF generate” option inside this page. I installed this plugin but it works fine with all pages except my custom page:

    https://bestwebsoft.com/products/wordpress/plugins/pdf-print/

    Someone knows why or if there are another plugoin for custom PHP pages ?

    Thanks, Xavi

    Thread Starter Xavierator

    (@xavierator)

    Dear again,

    I think that I know where is the problem, but I can’t resolve it.

    My custom PHP WP page https://www.tutusaus.cat/card-page/ has a button with a <a href label to another php file that uses tcpdf library, but this 2nd page don’t recognize something like:

    $wpdb->get_results

    How can I work with $wpdb object in the PDF generator page ?, maybe only with some include ?

    Many thanks, Xavi

    Moderator bcworkz

    (@bcworkz)

    Yes, you may be right. Unfortunately, it’s difficult to selectively load only a few WP resources. It’s usually all or nothing. How much data is involved in the return with the get_results() that you need to use? And is this the only WP resource you need on the 2nd page? I’m thinking the thing to do is pass the results as an URL parameter to the second page. The second page can then get the data out of $_GET and do what it needs with it. No need to get WP involved on this page.

    If it’s too much for an URL parameter, pass it as a POST field.

    If you need more WP resources on the second page, you could make that second page into a custom WP page template, then add a WP page based on it. Change the href target of the button to go to this WP page. This will cause all of the WP resources to be available and whatever PHP you have on this page will execute normally once you navigate to this page by its permalink.

    Thread Starter Xavierator

    (@xavierator)

    Dear,

    Thanks for your answer bcworkz.

    I only need the $wpdb resources and I like your second solution, call a WP second page instead of call directly de .php file

    I’ve been thinking in an another solution, all code in the same first php file, the pdf button will re-call to it self usong a parameter, something like this:

    if $_GET[‘DetectCall’]=’first_time’
    DoTheSameThanNow();
    else if $_GET[‘DetectCall’]=’pdf’]
    GeneratePDF()

    What do you think ?, do you know if it can work ?

    Thanks, Xavi

    Moderator bcworkz

    (@bcworkz)

    I don’t see why not. I don’t know what’s required for the PDF generation to work, but as I said, with a custom page template, any PHP code on the template will execute when the page is loaded, so the real challenge is ensuring the template has all the correct resources. Naturally, any PHP resources, like the declaration for GeneratePDF() can be loaded with the include or require statement. If there’s any jQuery or JavaScript needed, it needs to be enqueued with wp_enqueue_script(), which if done the usual way, loads for every page. You can add a conditional to only enqueue if get_queried_object_id() is equivalent to the page’s ID.

    Thread Starter Xavierator

    (@xavierator)

    Dear bcworkz,

    If I use your second solution and call the 2nd WP page name from <a href=… or if I use my proposed solution I get the same error in TCPDF library:

    Fatal error: Cannot declare class TCPDF, because the name is already in use in /homepages/25/d369985012/htdocs/html/tutusaus.cat/wp-content/themes/twentyseventeen/tcpdf/tcpdf.php on line 157

    I don’t know how to continue by this way ??

    Maybe the only solution (if it works) could be your first proposed solution, pass the information read with $wpdb->get_results() using an url parameters.

    Thanks, Xavi

    Moderator bcworkz

    (@bcworkz)

    That error means the class has already been declared as part of some other process, so you do not need to re-declare it. You can likely proceed with instantiating a new instance of that class or whatever needs to happen without your own class declaration.

    It’s not surprising that you will run into glitches like this when attempting to integrate previously established code into your own template. It generally doesn’t mean the approach is wrong, just that it needs to be adjusted. Hopefully you will not run into too many more. I’ll help if I can, but I know nothing of your PDF generation code, so I’m at a bit of a handicap.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘MySQL web page access’ is closed to new replies.