I have a website that uses WordPress. I would like to add in a post or a page a login code for an external database, code that uses two functions: 1. connect to the database 2. login form. I don't know how to do that, because if I add the code in a post, after doing the first action it halts. In the first php code I see that it calls another file, file that exists on server but after calling the file it halts. Thank you in advance.

  • It's unclear what you are asking. Both "pages" and "posts" are individual entries in the wp_posts database table - you can't put executable PHP code in either. Please review the "How to Ask" page in our help center for some guidelines for improving your question – bosco Mar 30 '17 at 6:38
  • I am sorry. I would like to know if there is any way to add executable PHP code in a post. I thought it is a plugin or something that would help me do that. – soldier896 Mar 30 '17 at 6:43
  • A quick Google search turns up some results. But please note that plugin recommendations are off-topic here. It's definitely possible to add the ability to execute PHP code via shortcode (or otherwise) yourself, however we ask that questions show a considerable amount of research and implementation effort on the asker's part. As it stands, it's unclear what you've tried thus far, and what problem you're running into. – bosco Mar 30 '17 at 6:52
  • I did some research and all I could find didn't help me because that php code didn't run the way it should. I used the php code in a shortcode but that also didn't run how it should. – soldier896 Mar 30 '17 at 6:56
  • Let's look at this in a different way. It sounds like you want a specific page to show some stuff based on some data from a different, non-WordPress database - is that correct? In short, what's the bigger goal that you are trying to accomplish? – bosco Mar 30 '17 at 6:58

One option would be to create a custom page template. Within that template, you can check for POSTed variables. Something to the effect of:

 * Template Name: Login form
if(!empty($_POST['username'])) {
    // check credentials and if valid display data
} else {
    // display login form

Once you've created this file and uploaded it in your theme files (preferably a child theme, if you are not using a custom-built theme; save it as something like tpl-login.php for easy reference) you can create a Page in WordPress and there will be a dropdown to select the template. Select this "Login form" template, publish the page or save a draft, and you can continue editing your page template until it performs all the functions you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.