• knorssman

    (@knorssman)


    I am creating my first plugin, which in a nutshell i need it to create a database, let the admin of the website using the plugin enter data into the database, then display some of that data for visitors to the website

    i have found out how to make custom database tables with $wpdb but i don’t understand how to display the data for users and how to let the website admin enter data

    thanks for any feedback

Viewing 10 replies - 1 through 10 (of 10 total)
  • adiant

    (@adiant)

    If practical, you will want to use the Setting API that WordPress built for plugins:
    https://developer.wordpress.org/plugins/settings/settings-api/

    Values are entered on Admin Panels and can be displayed for visitors to see with Shortcodes or other means.

    Moderator bcworkz

    (@bcworkz)

    Hmmm. I don’t agree that the Settings API is practical in this case. It’s really meant for saving settings in the options table.

    I personally would go with a custom page template, but page templates are meant for themes and child themes, not plugins. Still, templates can be made to work, especially if it’s not necessary for the template to be available for user created pages. Another way to run custom code in WP is by requesting pages through admin-post.php. It’s sort of like AJAX, but without JavaScript.

    If the page/custom template needs to be accessed through the admin menu, there’s a slight complication because the page content is supposed to be generated by a callback function. A rather clunky but functional solution is to have the menu callback output javascript that redirects to the desired page. Since the menu page will briefly appear, you may wish to also output something like “Forwarding…”.

    adiant

    (@adiant)

    Obviously, there is some confusion here that can only be clarified with more detail. You say “let the admin of the website using the plugin enter data into the database, then display some of that data for visitors to the website”.

    How much data? More details on it.

    Small amounts of data favour the Settings API approach. Large amounts of data favour creating your own tables either in the WordPress database or in a separate database.

    Thread Starter knorssman

    (@knorssman)

    the volume of data is not very high in practice, but it is an uncertain amount of data which i can’t know exactly how much while writing the code. the actual application is displaying league standings for whatever leagues the website admin is organizing, so the admin needs to be able to specify some attributes of the league and then add results of matches as easily as possible

    adiant

    (@adiant)

    In that case, I think that the Settings API will do the job for you a lot easier than having to code Forms and other stuff from scratch in HTML.

    Basic Concept of best use of Settings is to have only a handful of Settings, often just One, but each Setting can be a complicated Array of values. In PHP, you can have Arrays of Arrays, so there is no end to the level of Complexity you can create.

    The key piece of information I needed was to see if the data would fit in PHP memory, which it obviously will, so Arrays and other PHP variable types are appropriate.

    All that said, even the Settings API is going to leave you with a lot of PHP coding to get the job done.

    The performance will be top notch, too, as WordPress caches Settings.

    staartmees

    (@staartmees)

    Doesn’t advanced custom fields do the job?

    Moderator bcworkz

    (@bcworkz)

    ACF may work, depending on how you manage the teams in each league. If the teams are custom post types, then ACF should work famously, assuming ACF works with custom post types. It ought to, but I don’t use it. Which league the team belongs to could be managed with a custom taxonomy.

    Only a suggestion though, I think you should take a step back and think about how you want to organize the data. A completely custom data structure in bespoke tables does not leverage the power of WP functions. Imagine ways your data can fit within the current WP data schema.

    Thread Starter knorssman

    (@knorssman)

    i think what i would like to do is create some kind of webpage that is only admin accessible (do custom post types work?) with some html forms which then executes a php file on submission and performs operations on the $wpdb database, and using shortcodes to display the data

    i understand that this might be inefficient or like re-inventing the wheel, but i am much more familiar with html and $wpdb than the other APIs available in wordpress, i just need to have a place for the admin access html form to go

    Joey

    (@leglesslizard)

    This also may be a helpful link for you. It is using the settings API but is a step by step on how to implement. It covers just about everything and also has a link to Ottos blog who supplies really nice articles on wordpress also.

    Moderator bcworkz

    (@bcworkz)

    Inefficient coding or inefficient code? If the first, going with what you know is the most efficient. If the latter, not if well written, assuming you are staying in the WP environment either way. If one were truly after efficient code, using WP is not the best choice 🙂

    You can place an HTML form anywhere with a shortcode, either by putting it in content or by using echo do_shortcode('[my-shortcode]');. By using current_user_can() you can limit the form visibility to admin users.

    If you want to access this form from the backend menu, then the Settings API has merit, provided you want to use the options table for data. But then you want to avoid APIs, yes? Then just have your menu callback generate whatever HTML you desire. What’s possibly more important is where does your form submit to? If you use the Settings API, this is all taken care of for you. Otherwise the form’s action destination needs to initiate the WP environment some way.

    Requiring wp-load.php to do so is a bad idea. You might consider AJAX techniques, it makes for a nice user experience. The other options are going through admin-post.php or submitting to a published page based on a custom template.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘first plugin – how to let admin enter data and display data for users’ is closed to new replies.