Category: WordPress

WordPress Types Plugin output data sets using Get_Posts radio and checkboxes

You can use the WordPress Types Plugin to create custom groups of posts/pages type objects, which include the standard title, body WYSIWYG and featured image (or not), along with any number of any types of fields, checkbox, radio buttons etc. etc. 

Then you can use PHP/ Widgets on pages to output categories of data items

This will output all 

$pid = get_the_ID();
$argsDeals = array( 'post_type' => 'product-data-set', 'post_status' => 'publish','posts_per_page' => 50,
'meta_query' => array( array( 'wpcf-featured-item-list' => 'checkbox2' ,'value'=>'checkbox2' ,'compare' => 'LIKE' )) ,'meta_key' => 'wpcf-homepage-list-order','orderby' => 'meta_value_num','order' => 'ASC' );
$my_hotdeals = get_posts( $argsDeals );
foreach( $my_hotdeals as $post ) :
 setup_postdata($post);
 echo get_the_title($post->ID);
 echo get_permalink( $post->ID);
 if ( has_post_thumbnail($post->ID) ) {
 echo get_the_post_thumbnail($post->ID,'75square-thumb');
 }
 if (get_post_meta( $post->ID, 'wpcf-custom-text',true) != '') {
  echo get_post_meta($post->ID, 'wpcf-custom-text',true) ;
 }
endforeach;
wp_reset_query();

The text in bold is what you change depending on your custom posts/ fields. – they are in order:

  • Name of custom post type
  • Checkbox group with slug “featured-item-list
  • When the specific check box is ticked, the value is set to “checkbox2”
  • The homepage-list-order field is a numeric field for ordering the output
  • A text field with slug “custom-text”

You can also filter by comparing text fields or radio selectors (selecting a category)

It is the same as above, but instead you change the get post args:

$argsDeals = array( 'post_type' =>'product-data-set', 'post_status' => 'publish','posts_per_page' => 50,
 'meta_query' => array( array( 'wpcf-product-name' => 'yes', 'value' => 'item1', ) ),'meta_key' => 'wpcf-priority','orderby' => 'meta_value_num','order' => 'ASC', 'exclude' => $pid2 );

And the fields are similar except product-name is a radio button option which is matched to “item1” and priority is again a numeric field which orders the output.

To get a list of all checkbox values for a current product/ object – use this code to enter the checkbox values into an array:

$catlist = array();
if (get_post_meta( $post->ID, 'wpcf-featured-products',true) != '') {
 foreach (get_post_meta($post->ID, 'wpcf-featured-products',true) as $key => $value) {
 foreach ($value as $key2 => $value2) {
 $catlist[]=$value2;
 }
 }
}
foreach ($catlist as $key => $value) {
 echo $value . ' ';
}

This code would be inside the loop which considers and extracts data for one object

  • Checkbox list slug is featured-products
  • Array Featured-product ( Array $Key ( $Value2 = checkbox1 ) ) – loop through the inner array and copy to your own array to extract a list of category names
  • Print all the checkbox values for the current element you are considering

WordPress only show content for logged in admin user

Sometimes one might only want to show some content for logged in admin users, or a specific admin user – for testing purposes etc.

you can use this code:

<?php 
$current_user = wp_get_current_user();
if($current_user->user_email == "email@address.co")
{
  function(); // this function will only happen when this one admin user is logged in.
?>
<p>This HTML will only show when the user is logged in</p>
<?php
}

For content to be shown when a user is logged in as an admin in general, this can be used:

<?php if ( is_user_logged_in() ) {
  echo "This will be shown if a user is logged into the admin section.";
} ?> 

Various JavaScript Programming Notes

To make a 2 dimensional array

var v=new Array();
var v2=new Array();
v[0]=1; v[1]=7; v2[0]=v;
var x = v2[0][0] // x is 1;

Add to an array

v[v.length]=9;

Function – If string S is in array A, then return true

function inArray(string, array){
 for(var i = 0;i < array.length;i++){
 if (array[i]==string){
 return true;
 }
 }
 return false;
}

 

 

Featured Video rather than Image for WordPress

Some templates have a featured image, but sometimes you want a video instead… But not all the time..

You can set some custom fields, and then add in PHP – if the field has something in it – display the embedded video (at the same size the featured image would have been…)

The text in the custom field should just be the ID code for YouTube or Vimeo (or you can add your own if you want… but you need to add the embed code to the PHP)

The code gets the ID and inserts it into the embed code.

<?php
if (post_custom(‘a YouTube ID Code’)) {
echo ‘<iframe width=”640″ height=”402″ src=”https://www.youtube.com/embed/’ . get_post_meta($post->ID, ‘a YouTube ID Code’, $single = true) . ‘” frameborder=”0″ allowfullscreen></iframe>’ ;
}
else if (post_custom(‘a Vimeo ID Code’)){
echo ‘<iframe src=”http://player.vimeo.com/video/’ . get_post_meta($post->ID, ‘a Vimeo ID Code’, $single = true) . ‘?title=0&amp;byline=0&amp;portrait=0″ width=”400″ height=”225″ frameborder=”0″ webkitAllowFullScreen allowFullScreen></iframe>’ ;
}else{
?>

 

add the YouTube or Vimeo ID code to a custom field:

  • a YouTube ID Code
  • a Vimeo ID Code

EG:

  • bH9tJXm2Bcw
  • 31934775

From the original YouTube links:

  • https://www.youtube.com/watch?=bH9tJXm2Bcw&feature=channel_video_title
  • http://vimeo.com/31934775

Shift Joomla and WordPress installs

  • Copy HTML/CSS/etc. – folders. everything
  • Move the database

Login to old cPanel -> PHPmyAdmin
select databse, check all tables, “export” – all (custom – all options)

Go into new PHPmyAdmin
import all

edit (WP-config.php,. config.php (in Joomla)

Edit the database location:

  • Databse name (same)
    But – might be something like:
    old DBS name is “shop”
    new name is “rotapixt_shop”
  • Server (local host if it is on the same server, else the address (EG: http://cpanel.something.com)
    it’s possible to install the HTML files on one FTP account server, and install the database on a different server….
  • Database user username:  (hosting account username/ password login)
  • Database user password

Note:
In website hosting – you have an account username and password, this is the same for :

  • FTP account
  • cPanel login (maybe PHPmyadmin has a different password)
  • Database / MySQL username / password (would be the same as PHPmyAdmin)

Joomla – Configuration.php:

  • host $host
  • DB name $db
  • username MYSQL $user
  • pass $password

WordPress – wp-config.php

  • commented with labels ~

LOCAL HOST =

HTML goes in the htdocs folder

http://localhost/cpanel(then click “PHPMYADMIN” to edit database

  • DBS name = whatever you call it in PHPmyadmin
  • server = localhost
  • username = root
  • password = “” (nothing)

WordPress template editing Adding custom menu, sidebar, layout etc.

You can edit a WordPress template so that it is just a HTML file which inserts the body text, but you can use some powerful tools that make the template easier to edit, and more dynamic.

Custom Menu

You can use a custom menu to make the header links, sidebar links, footer links, or other list of links to pages.

You can add, remove, reorder these links in the WordPress admin section, so no need to sift through code, or edit HTML.

  • Just go to the admin section, click on Appearance -> Menus
  • Click the + to make a new menu
  • add a name
  • in the left hand side, you can choose what pages you want to add
  • then drag the items to reorder them
To print a menu, use:
<?php wp_nav_menu( array('menu' => 'Menu Name' )); ?>
This will print an unordered list of the menu items.
You can display them next to each other, change the colour of the current page, and more fancy things using CSS.

Custom Sidebars

You can add different things to the sidebar using widgets
WP-admin -> Appearance -> widgets
You can add a text widget to add your own HTML
This code can be added to your sidebar.php file to print the widgets

<?php
if ( 'content' != $current_layout ) :
?>
<div id="secondary" role="complementary">
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<aside id="archives" class="widget">
<h3 class="widget-title"><?php _e( 'Archives', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
</ul>
</aside>
<aside id="meta" class="widget">
<h3 class="widget-title"><?php _e( 'Meta', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<?php wp_meta(); ?>
</ul>
</aside>
<?php endif; // end sidebar widget area ?>
</div><!– #secondary .widget-area –>
<?php endif; ?>

 

Custom site layouts and structures

You can have different structures to your site. one page with no sidebars, one or two sidebars etc.

Just make different “page.php” files for each layout type, and you can set them in the page settings for each page.

just add
<?php
/*
Template Name: Snarfer
*/
?>

To your custom template

For more info: See the WordPress page on this topic

Custom CSS and Javascript in your Admin Section of WordPress

If you edit files in the admin folder of WordPress – you will lose all changes when you upgrade to a new WordPress version, so just add this to your functions.php file in your current WordPress theme.

/* ADDMING CSS TO ADMIN AREA OF WORDPRESS */
function custom_admin_css() {
echo '
<style type="text/css">
#footer{background:#acacac;}
</style>
';
}
add_action('admin_head', 'custom_admin_css');
/* end - ADDMING CSS TO ADMIN AREA OF WORDPRESS */

Adding a second loop to your WordPress site

You can list blog posts anywhere in your website, by adding a second loop.

Use this code: (edit the category name and number of posts to show – you can also edit what the loop shows, just listing the names of posts, linking to them, or add the summary/content…)

<?php rewind_posts(); ?>

<?php query_posts(‘category_name=NAME&showposts=5′); ?>

<?php while (have_posts()) : the_post(); ?>
<!– Do featured stuff… –>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
<?php // the_excerpt(); ?>
<?php //<div class=”entry”>
// the_content();
//</div>
//</div> ?></li>
<?php endwhile;
//wp_reset_query();
?>
</ul>

<p class=”right”><a href=”/NAME/”>All posts</a></p>

The last link is just a link to the category name so you can show all.

Reset WordPress Admin password through FTP

Update 2025 – if you know your username, you can set the password to ‘password’ and then log in and reset your password

$username = 'myadminusername'; // Replace with the actual username
$user = get_user_by('login', $username);

if ($user) {
wp_set_password('password', $user->ID); // Set new password
echo "Password reset for user: " . esc_html($user->user_login);
} else {
echo "User not found.";
}

Edit the “functions.php” file (wp-content -> themes -> name-of-current-theme -> functions.php

And add the following code to the beginning of it – before everything else:

<?php   
  wp_set_password('password',1);  
  $user_info = get_userdata(1);  
  echo 'Username: ' . $user_info->user_login . "\n";
?>

Then open your website, and it will show your 1st admin user’s username printed somewhere at the top of your website.

The password for that user account will now be “password”.

Delete the above code from “functions.php”, and login to your admin account, changing your password.

Resetting your password in Joomla

With Joomla, you aren’t that lucky… Joomla is slightly more secure, and quite the bit more annoying. They don’t provide a function to email yourself the password, so you need to edit the database. You can do the following

  1. Login to your MySQL Database using phpMyAdmin
  2. Go to your Joomla Users table, EG: jos_users
  3. Select the record (or table row) for your administrator account (The first admin account, created by default, has an id of 62)
  4. Click on ‘Edit Record’ or ‘Edit Inline’
  5. Select the drop-down “function” of “PASSWORD” on the right
  6. And add 5f4dcc3b5aa765d61d8327deb882cf99 as the new password
  7. Save
  8. Login to your site using the password “password” – site.com/administrator
  9. Then change your password when you login

One wordpress install with two domains pointing to it

There are some wp-config codes you can use to allow any domain to run on your website – but this is a method of adding exactly one specific domain name added on to your site. Multiple domains, one WordPress installation.

Open WP-CONFIG.PHP – and paste this after the line about “table prefix”

$hostname = $_SERVER['SERVER_NAME'];
$hostname = str_replace('http://', '', $hostname);
$hostname = str_replace('https://', '', $hostname);
$hostname = str_replace('www.', '', $hostname);
$hostname = strtolower($hostname);
if(substr($hostname, -1) == '/') {
 $hostname = substr($hostname, 0, -1);
}
if ($hostname == 'pushka.com') {
define('WP_SITEURL', 'https://'.$hostname);
define('WP_HOME', 'https://'.$hostname);
}

Replace “pushka.com” with your website address. This should be the 2nd add on domain (for example, if I wanted the website wordpress.com to appear at the address “pushka.com” also.