1

I tried reading the WordPress codex about integrating WordPress into an existing website.

I tried using the header information

<?php /* Short and sweet */ define('WP_USE_THEMES', false);
require('./wp-blog-header.php'); ?>

And then the test code

<?php // Get the last 3 posts.
 global $post; $args = array( 'posts_per_page' => 3 ); 
$myposts = get_posts( $args ); 
foreach( $myposts as $post ) :  setup_postdata($post); ?> 
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br /> 
<?php endforeach; ?>

Another code I added to make the page only for logged in users:

if ( ! is_user_logged_in() ) { // Display WordPress login form:
    wp_login_form($args);

This code does make the login show, but when I log in, it cannot remember that it is logged in, and the functions (blog posts) will not show. The funny thing is, the login form (as called by the wp_login_form) shows, but I cannot log in. This is quite anoying, as the information in the codex doesn't work Integrating Wordpress with Your Website

It does not work. Can someone help me? I tried posting at the WordPress forum, but no help there

New edit 07th november: This is how the PHP file looks like now. It's called lite_index.php and includes the wp-load, and because of that I believe the login form under here shows, bt still it cannot login.

<?php
//define('WP_USE_THEMES', false);
//require('lite/wp-blog-header.php');
define('COOKIEPATH', '/');
require('lite/wp-load.php');
require('loggin_check.php');
//require(dirname(__FILE__) . 'lite/wp-load.php');
//header('Content-Type: text/html; charset=ISO-8859-1');

$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>    
<?php the_excerpt(); ?> 
<?php
endforeach;
?>
<h1>Lite v4</h1>

<?php
if ( ! is_user_logged_in() ) { // Display WordPress login form:
    wp_login_form($args);
} else {
    wp_loginout(index.php); // Display "Log Out" link.
    echo " | ";
    wp_register('', ''); // Display "Site Admin" link.
    echo "<br />\n";
}
?>
  • The first thing I notice is, that your require('./wp-blog-header.php'); makes it look like that your WordPress-files are in the root-directory (amongst your other files). I think I would keep WordPress in a folder by itself, to avoid wierd collisions. I'm not sure if that's the right way to do it, though. ... Another thought that pops into my head is, that have you considered doing it the other way around (by putting your website into a theme, inside WordPress). I used to make websites like you describe, and it becomes a mess. But that's just my personal preference. – Zeth Nov 6 '18 at 0:49
  • Thank you @Zeth for your comment. I edited my question with an appendix, which includes the code for my index file as it works now. You say that you formerly made websites like this. Do you have any examples of how that worked? – Ole Lasse Bjellaas Nov 7 '18 at 15:12
1

I started writing a pretty long comment, but I figured I would write it as a solution instead.

If you mix all your files in one big jumble, then it probably wont work. But if you put WordPress in a subdirectory, then it would work (that's how I used to do it).

REMEMBER!!! If you already have a working WordPress-installation and want to move the posts, then you can't simply move the files. You'd have to follow the right procedure, which is explained here.

Example of correct file-structure, mixing a static website with WordPress:

|-your-index.php
|-your-page-1.php
|-your-page-2.php
|-your-css
|  |-your-styles.css
|
|-your-js 
|  |-your-js-file.js
|
|-wp     <-- Put your entire WP-installation in a folder
   |-wp-content
   |      |-themes
   |      |-uploads
   |      |-...
   |      |-...
   |      
   |-wp-includes
   |      |-...
   |      |-...
   |
   |-wp-admin
   |      |-...
   |      |-...
   |
   |-wp-config.php
   |-wp-blog-header.php
   |-...
   |-...

This way you can call

<?php require( $_SERVER['document_root'] . '/wp/wp-blog-header.php'); ?>

Then you can keep your own static files and your WordPress-installation seperate and make it work. This is how I used to do it, and that worked.


Flipside

I can see (by Googling a bit), that it looks like that @Bob's suggestion has become more popular. Here's an example, using wp-load. I must admit, that I don't know what the difference is between the two.

This solution suggest a third way of doing it.

But all things considered... Regardless of which way you load WordPress, then I think that it's your file structure that is your current challenge (if I'm not mistaken). WordPress needs it's own isolated environment (folder), so it can make it's URL and call the files as intended.


Final word

But as I suggested in my comment. I used to do this, - and I wish I never did. I wish I had baked my static website into a WordPress-theme.

  • Thank you so much @Zeth for your answer. I did after reading through all your comments, and links to articles, find out that if I put my code withing the wordpress directory, it works. So I have found a workaround it seems. But I found it strange, that I cannot include wordpress as a subdirectory – Ole Lasse Bjellaas Nov 9 '18 at 8:34
1

Have you tried using the following?

require(dirname(__FILE__) . '/wp-load.php');

Note that this requires WordPress to be installed in the root directory of the website.

  • It is installed in a subdue, called "lite" – Ole Lasse Bjellaas Nov 2 '18 at 9:57
  • Sorry auto correct, ment sub dir – Ole Lasse Bjellaas Nov 2 '18 at 9:57
  • Then it should probably look like this: require( 'your-website-root-directory/lite/wp-load.php'); – Bob Nov 2 '18 at 10:50
  • Added a code example in my original question. But I found out that I have managed to include wp-load all along, but the functions are still lacking. – Ole Lasse Bjellaas Nov 5 '18 at 19:05
  • Looking more closely to wp-load.php, it doesn't seem to run the WordPress query- as explained here: stackoverflow.com/questions/13860577/wordpress-wp-load-php . So you'd need to initialize a new WP_Query in order for the posts to be displayed. You can find various examples here: code.tutsplus.com/tutorials/… – Bob Nov 8 '18 at 8:17

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.