Thursday 19 January 2012

How to write sql query and form in wordpress plugin at admin end

When writing plugins, it's a good idea to keep your data-handling within the plugin's main file (i.e. not sending it to a separate file), and activate functions to handle it accordingly. Basically, you can set your form's action to point to either the plugin's file, or the page that contains the form. else following error will occur.

Fatal error: Call to a member function get_results() on a non-object (jQuery From Plugin & WordPress)
the main plugin file which appear as plugin page , always write sql quries there you need put some flag $_POST['flag'] to distingush quries coming from which section.

<form id="frmfbdata" name="frmfbdata" method="POST" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
 <input type="text" id="username"  name="username" value="">
 <input type="text" id="useremail" name="useremail" value="">
 <input type="text" name="fconnect" id="fconnect" value="y">
 </form>
 
global $wp_table,$wpdb;
$email=$_POST['email'];
$name=$_POST['name'];
if($email!=""){
 $querystr = "SELECT count(*)as cnt FROM $wpdb->options WHERE $wpdb->options.option_name = 'addbanner'";
 $result = $wpdb->get_results($querystr);
 //**Array ( [0] => stdClass Object ( [cnt] => 0 ) )

$recordcount=$result[0]->cnt;
 if($recordcount==0){
  $mydata=array();
  $mydata['email']=$email;
  $mydata['name']=$name;
  $data=json_encode($mydata);
  $query = "INSERT INTO $wpdb->options SET blog_id='0',option_name='addbanner',option_value='".$data."',autoload='no'";
  $wpdb->get_results($query);
 }
}


ajax query to a page works but if sql query is written it shows fatal error
 
function
var name=document.getElementById("username").value;
var email=document.getElementById("useremail").value;
document.getElementById('frmfbdata').submit();
var ajaxurl = "<?php bloginfo('wpurl') ?>/wp-content/plugins/addbanner/myajax.php";
var data = {
email: email,
name: name
};


//** since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php

jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});

}callajax(){

FaceBook Fconnect with SDK

<div id="fb-root"></div>
        <script type="text/javascript">
            var button;
            var userInfo;

            window.fbAsyncInit = function() {
                FB.init({ appId:  'APP_API_ID',
                    status: true,
                    cookie: true,
                    xfbml: true,
                    oauth: true});

               showLoader(true);

               function updateButton(response) {
                    button       =   document.getElementById('fb-auth');
                    userInfo     =   document.getElementById('user-info');

                    if (response.authResponse) {
                        //user is already logged in and connected
                        FB.api('/me', function(info) {
                            login(response, info);
                        });

                        button.onclick = function() {
                            FB.logout(function(response) {
                                logout(response);
                            });
                        };
                    } else {
                        //user is not connected to your app or logged out
                        button.innerHTML = 'Login';
                        button.onclick = function() {
                            showLoader(true);
                            FB.login(function(response) {
                                if (response.authResponse) {
                                    FB.api('/me', function(info) {
                                        login(response, info);
                                    });
                                } else {
                                    //user cancelled login or did not grant authorization
                                    showLoader(false);
                                }
                            }, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'});
                        }
                    }
                }

                // run once with current status and whenever the status changes
                FB.getLoginStatus(updateButton);
                FB.Event.subscribe('auth.statusChange', updateButton);
            };
            (function() {
                var e = document.createElement('script'); e.async = true;
                e.src = document.location.protocol
                    + '//connect.facebook.net/en_US/all.js';
                document.getElementById('fb-root').appendChild(e);
            }());

            function login(response, info){
                if (response.authResponse) {
                    var accessToken                                 =   response.authResponse.accessToken;

                    userInfo.innerHTML                             = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + info.name
                                                                     + "<br /> Your Access Token: " + accessToken;
                    button.innerHTML                               = 'Logout';
                    showLoader(false);
                    document.getElementById('other').style.display = "block";
                }
            }

            function logout(response){
                userInfo.innerHTML                             =   "";
                document.getElementById('debug').innerHTML     =   "";
                document.getElementById('other').style.display =   "none";
                showLoader(false);
            }

            //stream publish method
            function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){
                showLoader(true);
                FB.ui(
                {
                    method: 'stream.publish',
                    message: '',
                    attachment: {
                        name: name,
                        caption: '',
                        description: (description),
                        href: hrefLink
                    },
                    action_links: [
                        { text: hrefTitle, href: hrefLink }
                    ],
                    user_prompt_message: userPrompt
                },
                function(response) {
                    showLoader(false);
                });

            }
            function showStream(){
                FB.api('/me', function(response) {
                    //console.log(response.id);
                    streamPublish(response.name, 'I like the articles of Thinkdiff.net', 'hrefTitle', 'http://thinkdiff.net', "Share thinkdiff.net");
                });
            }

            function share(){
                showLoader(true);
                var share = {
                    method: 'stream.share',
                    u: 'http://thinkdiff.net/'
                };

                FB.ui(share, function(response) {
                    showLoader(false);
                    console.log(response);
                });
            }

            function graphStreamPublish(){
                showLoader(true);

                FB.api('/me/feed', 'post',
                    {
                        message     : "I love thinkdiff.net for facebook app development tutorials",
                        link        : 'http://ithinkdiff.net',
                        picture     : 'http://thinkdiff.net/iphone/lucky7_ios.jpg',
                        name        : 'iOS Apps & Games',
                        description : 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'

                },
                function(response) {
                    showLoader(false);

                    if (!response || response.error) {
                        alert('Error occured');
                    } else {
                        alert('Post ID: ' + response.id);
                    }
                });
            }

            function fqlQuery(){
                showLoader(true);

                FB.api('/me', function(response) {
                    showLoader(false);

                    //http://developers.facebook.com/docs/reference/fql/user/
                    var query       =  FB.Data.query('select name, profile_url, sex, pic_small from user where uid={0}', response.id);
                    query.wait(function(rows) {
                       document.getElementById('debug').innerHTML =
                         'FQL Information: '+  "<br />" +
                         'Your name: '      +  rows[0].name                                                            + "<br />" +
                         'Your Sex: '       +  (rows[0].sex!= undefined ? rows[0].sex : "")                            + "<br />" +
                         'Your Profile: '   +  "<a href='" + rows[0].profile_url + "'>" + rows[0].profile_url + "</a>" + "<br />" +
                         '<img src="'       +  rows[0].pic_small + '" alt="" />' + "<br />";
                     });
                });
            }

            function setStatus(){
                showLoader(true);

                status1 = document.getElementById('status').value;
                FB.api(
                  {
                    method: 'status.set',
                    status: status1
                  },
                  function(response) {
                    if (response == 0){
                        alert('Your facebook status not updated. Give Status Update Permission.');
                    }
                    else{
                        alert('Your facebook status updated');
                    }
                    showLoader(false);
                  }
                );
            }

            function showLoader(status){
                if (status)
                    document.getElementById('loader').style.display = 'block';
                else
                    document.getElementById('loader').style.display = 'none';
            }

        </script>

        <h3>New JavaScript SDK & OAuth 2.0 based FBConnect Tutorial | Thinkdiff.net</h3>
        <button id="fb-auth">Login</button>
        <div id="loader" style="display:none">
            <img src="ajax-loader.gif" alt="loading" />
        </div>
        <br />
        <div id="user-info"></div>
        <br />
        <div id="debug"></div>

        <div id="other" style="display:none">
            <a href="#" onclick="showStream(); return false;">Publish Wall Post</a> |
            <a href="#" onclick="share(); return false;">Share With Your Friends</a> |
            <a href="#" onclick="graphStreamPublish(); return false;">Publish Stream Using Graph API</a> |
            <a href="#" onclick="fqlQuery(); return false;">FQL Query Example</a>

            <br />
            <textarea id="status" cols="50" rows="5">Write your status here and click 'Status Set Using Legacy Api Call'</textarea>
            <br />
            <a href="#" onclick="setStatus(); return false;">Status Set Using Legacy Api Call</a>
        </div>

materials supplied by

Wednesday 18 January 2012

To make localhost as live IP for fconnect

Some times we are working in localhost and need FB fconnect to authenticate an user, for that do simple step to add an domain name to your localhost. just open C:\WINDOWS\system32\drivers\etc\hosts as window administrator admin user.  content in file would look like below

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

184.0.0.1       localhost
184.0.0.1       example.com


just add another line below local host  as example.com, check in browser instead of localhost type example.com we will get the WAMP default page

Wednesday 4 January 2012

How to get YouTube Thumbnail images of a video

To show a video at website from youtube share video link, http://youtu.be/Sl9fipWZ6E0 .
we use following object code
<object width="495px" height="290px">
   <param name="movie" value="http://youtu.be/Sl9fipWZ6E0">
   </param><param name="allowFullScreen" value="true"></param>
   <param name="allowscriptaccess" value="always"></param>
   <embed src="http://youtu.be/Sl9fipWZ6E0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="495px" height="290px">
   </embed></object>

Adode flash should be installed at your system to run the shock player.

now to show thumbnail below that video , we use VIDEO_ID - S19fipWZ6E0
http://img.youtube.com/vi/VIDEO_ID/#.jpg

where # is 0,1,2,3 .. where 2 is the default image
so our HTML code would be 
<div>
<img src="http://img.youtube.com/vi/Sl9fipWZ6E0/2.jpg">
</div>

a thumbnail image as above will display at the page