WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Learning environment for PHP Functions and Loop (8 posts)

  1. paulbbird
    Member
    Posted 1 year ago #

    Hi

    I am a bit past the newbie stage, and have got my first site up and running and generally making progress with WP.

    I have also set up WAMP server and can access my test site OK, and can get to my MySql console fine.

    I am now learning about the Loop and basic functions, (using the codex and web articles) When I want to test out a bit of code I am doing the following:

    1: I have copied a theme template in my test theme folder to experiment on
    2: I am typing the code snippets into the template using my Text editor (sublime 2)
    3: I save the theme and then preview the post or page in my test WP site to see what effect the code changes have made.

    Is this the best way to develop / test (and learn)?
    Or should I be using something else - say console based approach or other tools?
    How do developers set things up?

    Thanks P

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    Generally speaking, yes - that's exactly the best way to proceed. Is this theme your own custom theme or one you downloaded from elsewhere?

  3. keesiemeijer
    moderator
    Posted 1 year ago #

    Always develop on your local server. If you're just testing code this is the way to do it (develop / test (and learn)).
    Maybe creating a child theme instead of editing your theme directly would be better.

    If you are developing a plugin or theme (or something else) I would use some kind of version control:
    http://betterexplained.com/articles/a-visual-guide-to-version-control/
    A popular one is github:
    http://en.wikipedia.org/wiki/GitHub
    https://github.com/

    Here is a good tutorial for setting up sublime 2 for php:
    http://blog.stuartherbert.com/php/2012/02/28/setting-up-sublime-text-2-for-php-development/

    Another tip is just to look into the code of WordPress and see how everything is working (never edit it).

  4. paulbbird
    Member
    Posted 1 year ago #

    Hello guys

    Thanks for the feedback which I really appreciate.

    The theme I am using for my learning experience is a premium theme (the same I am using on my live site)

    I am mainly trying to learn how to display my content in different ways and so loop and functions are my current focus (rather than theme development itself)

    One example of the stuff that confuses me is this. One book I am reading (Professional WP Plugin development by Brad Williams) says stuff like this:

    Check out which custom plugins are registered in your WP site by running this: CODE SNIPPET

    I understand the code snippet fine - but where do I run it in order to get the output? Should I be in some sort of console mode?

    Thanks P

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    Can you post the code snippet?
    Without having access to the book it's difficult to know where the snippet should go. Probably somewhere in your plugin (as it's about plugin development) or maybe in your theme's functions.php file.
    http://codex.wordpress.org/Functions_File_Explained

    If you want to see what's stored inside a variable you can just echo it:

    <?php
    $var = some_function();
    echo $var;
    ?>

    If $var is an array it will echo:

    Array

    To see what is in an array you can use:

    <?php
    $var = some_function();
    echo '<pre>';
    print_r($var);
    echo '</pre>';
    ?>

    Be aware that printing out a variable can sometimes break your site (but your developing on a local host so you're okay).

  6. paulbbird
    Member
    Posted 1 year ago #

    Hi Keesiemeijer

    Really appreciate your help and links so far.

    Here is an example of the things that are confusing me. I can understand the code OK, but where to run it?

    Heres the example from the book:
    To return a list of all registered post types in WordPress, you’ll use the get_post_types() function. <? php get_post_types( $ args, $ output, $ operator ); ?>

    Williams, Brad; Damstra, David; Stern, Hal (2012-12-17). Professional WordPress: Design and Development (Kindle Locations 3437-3439). Wrox. Kindle Edition.

    Paul

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    Look at the codex page for that function:
    http://codex.wordpress.org/Function_Reference/get_post_types

    Look at the parameters section. It tells you what parameters are optional or required. None of the parameters are required for this function. This means you can use the function without any of the parameters (it will use the default values):

    $var = get_post_types();

    If you do provide parameters (other than the default) the returned results will change. Try and use the examples from the codex page (or from your book) in the copied theme template and see the difference.

    Let's take a look at another function:
    http://codex.wordpress.org/Function_Reference/get_category
    If you look at the parameters you see that that $category is a required parameter. This means it doesn't return a value (or some functions throw an error) if you use it without the required parameter:

    // without the ID parameter
    $var = get_category();

    By reading the codex you now now it needs a category ID:

    $var = get_category(22);

    Most codex functions pages in the codex also provide the return values of a function.

    OK, but where to run it?

    That depends on the function but generally you can use the functions on all your theme template files or plugin files. Be aware that returned results may differ on what template file you are using it. (e.g. is_category() will only return true on a category request (page))

    Just try and test functions that return something with the examples to print out a variable above.

  8. paulbbird
    Member
    Posted 1 year ago #

    Keesiemeijer

    Thanks for all your help. You have really tried to point me in the right direction which is appreciated

    Thanks
    Paul

Topic Closed

This topic has been closed to new replies.

About this Topic