1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Interact with wordpress db using php

Discussion in 'PHP' started by adithya, Aug 3, 2008.

  1. #1
    Hi

    I am thinking is it possible to post the article not via wordpress admin panel but by using php and some mysql commands...?

    So any hints,suggestions regarding how to interact with wordpress cms directly by using php script and edit or post the articles...?
     
    adithya, Aug 3, 2008 IP
  2. david_t

    david_t Member

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    45
    #2
    I assume the site in question is on a host where you have access rights, right? Then you should either see if you have the possibility to install/use phpmyadmin or mysql-query-browser.

    phpmyadmin is php software for administrating your mysql-databases (and needs to be installed on the same machine). Whilst mysql-query-browser can be used (if the database server accepts connections who aren't from localhost) as an ordinary stand alone software.
     
    david_t, Aug 3, 2008 IP
  3. adithya

    adithya Well-Known Member

    Messages:
    568
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Yeah i am talking about my site itself...
    i know phpmyadmin a little bit...
    I Have a basic knowledge of php and mysql ...
    Could u please elaborate what are the things that i must do...?
     
    adithya, Aug 3, 2008 IP
  4. MartinGr

    MartinGr Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Pretty much all you need to do is to create an interface for submitting content to database and a query for it.
     
    MartinGr, Aug 3, 2008 IP
  5. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    here's a small snippet to post to wordpress
    (that's about as simple as it gets)

    
    
    require_once('../wp-config.php');
    require_once('../wp-includes/post.php');
    require_once('../wp-includes/wp-db.php');
    
    	class wm_mypost {
    		var $post_content;
    		var $post_title;
    		var $post_status;
                    var $post_author;
    	}
    
    	$wm_myobject = new wm_mypost();
    
            $wm_myobject->post_content = 'my first content';
            $wm_myobject->post_title = 'hello world';
    	$wm_myobject->post_status = 'publish';
    	$wm_myobject->post_author = 1;
    
    	wp_insert_post($wm_myobject);
    
    
    PHP:
    mysql data (host, dpassword, database) is stored in wp-config.php
    the wordpress database is declared in wp-db.php
    and wordpress post functions in post.php

    so i include the function files and have access to all relevant wordpress functions and the database,
    so then I want to use the the wordpress 'wp_insert_post' function,
    which requires an object as input

    I define the wm_mypost class (content, title, author, status) and add content, add a title, put author on 1 and the status on 'publish',
    then i hand the object to the wp_insert_post function and wordpress handles it as a regular posting.

    attention point is the presence of 'hooks',
    if you use plugins like google-sitemap these are triggered by 'publishing', so wordpress will also make a sitemap (can take 2 to 10 seconds).

    that's one way of doing that.
     
    juust, Aug 3, 2008 IP
    adithya likes this.
  6. adithya

    adithya Well-Known Member

    Messages:
    568
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    110
    #6
    ^^IT WORKS :D
    This is exactly what i wished .... :)
    Thanks a ton juust :)
    BTW one more little thing...
    can we do schedule posting by using this snippet...?
    for example to post after 2 hrs some thing like that ...
     
    adithya, Aug 3, 2008 IP
  7. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i haven't tried that one,
    but it seems you can use post_date and set it to a future date,

    when you submit it the post is already 'published' but marked in the database with the post_status 'future', and will not show in the blog until the future time.

    I add $post_date to my class,

        
    class wm_mypost {
            var $post_content;
            var $post_title;
            var $post_status;
            var $post_author;
    
            var $post_date;
    
        }
    
    PHP:
    then I make a date based on now '+1 day',
    format it for wordpress,
    and put it in the post-array :

    
    $future_date = strtotime('+1 day');
    $formatted_date = strftime("%Y-%m-%d %H:%M:%S", $future_date);
    $wm_myobject->post_date = $formatted_date;
    
    PHP:
    that should do it ? the post should appear in the admin section under 'manage', 'posts'

    full listing :
    
    
    require_once('../wp-config.php');
    require_once('../wp-includes/post.php');
    require_once('../wp-includes/wp-db.php');
    
    class wm_mypost {
            var $post_content;
            var $post_title;
            var $post_status;
            var $post_author;
            var $post_date;
        }
    
        $future_date = strtotime('+1 day');
        $formatted_date = strftime("%Y-%m-%d %H:%M:%S", $future_date);
    
        $wm_myobject = new wm_mypost();
    
        $wm_myobject->post_content = 'my first content';
        $wm_myobject->post_title = 'hello world';
        $wm_myobject->post_status = 'publish';
        $wm_myobject->post_author = 1;
        $wm_myobject->post_date = $formatted_date;
    
        wp_insert_post($wm_myobject);
    
    PHP:
     
    juust, Aug 3, 2008 IP