is_home() is not working in functions.php

Sangwan Pankaj Reply 04:15
At the time functions.php is included during bootup, WordPress has no idea on the contents of the query, and doesn't know the nature of the page. is_home will return false.

Wrap the code in a function and have it triggered by the wp hook, which comes after the global query object has been hydrated with data.


add_action( 'wp', 'check_homepage' );
function
check_homepage() {
    if ( is_home() )
        add_action( 'wp_enqueue_scripts', 'my_scripts' );
}

Limit Words and Characters in content WordPress

Sangwan Pankaj Reply 21:22
add_filter('the_content', 'trim_content');

// This function use to modify you content functions
function trim_content($content){
  $word_limit =30;
  $words = explode(' ', $content);
  return implode(' ', array_slice($words, 0, $word_limit));
}


OR

add_filter('the_content', 'trim_content');

// This function use if  the content having more than 100 charaters it converts into excerpt format.
function trim_content($content)
{
    if(is_archive())
    {
       
// I'm getting the first 100 characters just to show an example you can change the value to get the words..
        $content = (strlen($content) <= 100)? $content : wp_html_excerpt($content, 100);
    }

    return $content;
}

Disable Plugin and Theme Update and Installation

Sangwan Pankaj Reply 13:02
If you really want to disable Updates and Installations, you can block users from installing/updating themes and plugins through the dashboard. Add this quick snippet to your wp-config.php file:

define('DISALLOW_FILE_MODS',true);

It will prevent users from installing and updating themes and plugins. It will also automatically disable theme and plugin editing in the dashboard.


Disable the Plugin and Theme Editor

Sangwan Pankaj Reply 12:57
Access to plugin and theme code is available in the WordPress dashboard. You can do one thing to protect the site from trifile to disable the both of these editors. Open your wp-config.php file and add the following constant:

define('DISALLOW_FILE_EDIT',true);

Now, when you are in the dashboard it is impossible to access the theme or plugin editor, even if you are admin.

Enable Send Email in XAMPP (localhost)

Sangwan Pankaj Reply 11:27
In this tutorial, i will share how to send Email from XAMPP (PHP). i use XAMPP 1.8.1 version .This has been tested by me and it worked.

Lets start:-
first always make a backup before changing any code in core/configuration files.
then

Edit your php.ini in - “xampp\php\php.ini”

Before Changes:- 
---------------------------
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost

; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesD:\xampp) fakemail and mailtodisk do not work correctly.
; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.

; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
;sendmail_path = "\"\XAMPP\sendmail\sendmail.exe\" -t"

After Changes :-
-----------------------
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 587

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = yourgmailid@gmail.com

; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesD:\xampp) fakemail and mailtodisk do not work correctly.
; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.

; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
sendmail_path = "\"\XAMPP\sendmail\sendmail.exe\" -t"

now

Edit your sendmail.ini - “xampp\sendmail\sendmail.ini”

smtp_server=mail.mydomain.com or smtp_server=localhost

; smtp port (normally 25)
smtp_port=25

comment these two lines using - ;


;smtp_server=mail.mydomain.com or ;smtp_server=localhost
; smtp port (normally 25)

;smtp_port=25



and add some extra code in the bottom of your sendmail,ini:

;new config:
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
pop3_server=
pop3_username=
pop3_password=
force_recipient=
hostname=
auth_username=yourgmailid@gmail.com
auth_password=your gmail password
force_sender=yourgmailid@gmail.com

NOTE: 
  1. You Need to restart xampp after saving both files.
  2. Change yourgmailid@gmail.com & your gmail password  with your own email id and password
  3. You Need to enable SMTP in your GMail to make this work and all sender will be from your email (if need).





How to get working website path & directory name?

Sangwan Pankaj Reply 12:35
Hi, in this tutorial we will discuss on current website url and current / working directory name

First you can get the name of the website by using

<?php echo $_SERVER['SERVER_NAME']; ?>
or
<?php echo $_SERVER['HTTP_HOST']; ?>

Now you can get the directory name

<?php echo dirname($_SERVER['PHP_SELF']); ?>

NOTE:
(if you are working on  multi folders and you need parent folder you can use 'dirname()'  function multi times

Example if you using 2 folders like /manage/user/ and you need parent folder only (i.e manage)

<?php echo dirname(dirname($_SERVER['PHP_SELF'])); ?>

you will get /admin result.

 )

Now the complete path

<?php echo "http://".dirname($_SERVER['SERVER_NAME']."".$_SERVER['PHP_SELF']); ?>

Result:  http://www.example.com/manage/user

and

complete the path with parent directory name

<?php echo "http://".dirname(dirname($_SERVER['SERVER_NAME']."".$_SERVER['PHP_SELF'])); ?>

Result:  http://www.example.com/manage






Pagination function in wordpress for posts

Jimmy Wales Reply 14:55
Put this code in functions.php file
<?php

if ( ! function_exists( 'wp_pagination' ) ) :
function wp_pagination() {
global $wp_query;

$int_val= 99999999; // need an unlikely integer

echo paginate_links( array(
'base' => str_replace( $int_val, '%#%', esc_url( get_pagenum_link( $int_val) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;

?>

and put this code anywhere for pagination

<?php wp_pagination (); ?>
Copyright by GhostPHP. Powered by Blogger.

Search

Recent Post

Popular Posts

Follow us