Category: WordPress

These days WordPress power more than 40% of all web pages. Sooner or later you will have to use a WordPress solution – and want to tweak it. Here are my tips and tricks for advanced WordPress developers.

  • Images in Child Themes / Themes

    How to display images in a theme or childtheme in WordPress.

    On Linux and perhaps some Mac systems you may have to change the owner of the folder to daemon:daemon and set chmod to 777 during development.

    When you launch the site online the file permissions must be more strict. Make sure that:

    • All directories should be 755 or 750.
    • All files should be 644 or 640. Exception: wp-config.php should be 440 or 400 to prevent other users on the server from reading it.

    Sometimes the images will not display because of user rights. On Linux and perhaps Mac you may have to set the owner to daemon and the usergroup to daemon:

    • # sudo chown -R daemon:daemon yourWordPressFolder/
  • Know WordPress

    1. Create some posts. Just some text that comes to your mind.
    2. Add an image to the post.
    3. Why not share a video from YouTube.
      Tip: just paste the share link into your post. Save and see …
    4. Create a page. Adorn with videos and images you fancy.
    5. Add your page to the menu.
    6. Go to the frontend. Try out your menu.
    7. Write a post where you explain the difference between a PAGE and a POST. The intended target group is a teenager who sees WordPress for the first time.
    8. How can you add a POST to your menu?
    9. Try out the themes.
      1. Find alternative themes online.
      2. Install them.
      3. Choose one that you fancy.
    10. Upload a screendump (or screendumps) of your work to fronter. That’s the mandatory assignment for today.
  • Tutorials – the wpdb class

    Tutorials by Honey Boney.

    The wodb class

    Insert

     

    Create table

     

    Tutorial by Honey Boney on Youtube

  • WordPress theme with Bootstrap

    An excellent tutorial by Zac Gordon: “How to Build a Responsive WordPress Theme with Bootstrap”.  You’ll learn to hack a WP theme from scratch with Bootstrap and jQuery enabled.

  • Reset $_POST (in WP ??)


    function testSubmit()
    {
    var x = document.forms["myForm"]["input1"];
    var y = document.forms["myForm"]["input2"];
    if(x.value =="" || y.value=="")
    {
    alert('Not allowed!!');
    return false;
    }
    return true;
    }
    function submitForm()
    {
    if(testSubmit())
    {
    document.forms["myForm"].submit(); //first submit
    document.forms["myForm"].reset(); //and then reset the form values
    }
    }

  • WordPress – opsætning på EAL

    Eksempel

    • Bruger: xxxx.mmd.eal.dk
    • Password: *********
    • Database: xxxx_mmd_eal_dk

    Filen wp-config.php kommer til at se sådan ud:

    // ** MySQL settings – You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define(‘DB_NAME’, ‘xxxx_mmd_eal_dk’);

    /** MySQL database username */
    define(‘DB_USER’, ‘xxxx.mmd.eal.dk’);

    /** MySQL database password */
    define(‘DB_PASSWORD’, ‘*******’); // password hidden here 😉

    /** MySQL hostname */
    define(‘DB_HOST’, ‘localhost’);

  • Plugin CSS i WordPress

    Efter en række eksperimenter har jeg fundet denne opskrift, der hooker et stylesheet rigtigt ind i WP dashboard. Virker både for admin og øvrige registrerede brugere:

    add_action( 'admin_enqueue_scripts', 'safely_add_stylesheet_to_admin' );
        /**
         * Add stylesheet to the page
         */
        function safely_add_stylesheet_to_admin() {
            wp_enqueue_style( 'prefix-style', plugins_url('style_admin.css', __FILE__) );
        }
  • CSS i plugin

    Dokumentationen på Codex er ikke lysende klar; men et plugin gør sådan:

    /* filters etc. */
    add_action('wp_print_styles', 'add_hyperboard_stylesheet');
    
    // (...)
    
    /*Required stylesheets */
    function add_hyperboard_stylesheet() {
    	$myStyleUrl = WP_PLUGIN_URL . '/hyperboard/styles.css';
    	$myStyleFile = WP_PLUGIN_DIR . '/hyperboard/styles.css';
    
            if ( file_exists($myStyleFile) ) {
                wp_register_style('HyperBoardStyleSheets', $myStyleUrl);
                wp_enqueue_style( 'HyperBoardStyleSheets');
            }
    }

    Pluginet sætter en krog i wp_print_styles.

  • Sådan: $wpdb->update / insert

    $wpdb klassen bruges sådan (se Codex her):

    $wpdb->update(
       "KbnProjects", // tabellen
          array('Name' => 'Værdi'), // opdateringsfeltet
          array('ID' => 0) // Id 
    );

    Insert fungerer i øvrigt på en lignende måde:

    $wpdb->insert( 
    	'table', 
    	array( 
    		'column1' => 'value1', 
    		'column2' => 123 
    	), 
    	array( 
    		'%s', // streng
    		'%d' // tal
    	) 
    );

    Codex forklarer desværre ikke, hvordan et array skal se ud, når et felt har en autoincremented Id. I dette tilfælde indsætter man en NULL værdi. I praksis kunne det se sådan ud:

    	$wpdb->insert(
    		'MyTable',
    		array(
    			'Id' => NULL,
    			'KbnProjects_Id' => 1,
    			'Color' => 'Smurphy',
    			'Limit' => 3,
    			'State' => 'Nuser!'
    		),
    		array(
    			'%d',
    			'%d',
    			'%s',
    			'%d',
    			'%s'
    		)
    
    	);

    Værdierne kunne sætte ind fra en formular, fx

    'KbnProjects_Id' => $_POST['KnbProjects_Id'],
    'Color' => $_POST['Color'],
    ... etc ...
  • Adgangskontrol i WP

    Denne lille stump kode er meget nyttig:

    if ( !current_user_can( 'publish_posts' ) )  {
    		wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    	}

    Her er WP listen over brugerrettigheder: user_can “Roles and Capabilities” på Codex.

    En række af WPs hooks skriver “capability”. Hvis fx en Contributor skal have adgang til noget, så kan capability være edit_posts. Enkelt og praktisk.

     

  • Sti til fil

    // saadan laves stien til plugin:
    echo plugin_dir_url(__FILE__);

    Efterfulgt at sti til en fil fx

    // saadan laves stien til plugin:
     $fil = plugin_dir_url(__FILE__) . "minFil.php";
Enable Notifications OK No thanks

We use cookies - more information

Multimusen.dk will set a few cookies from Doubleclick, Google and the Social Media plugins they ay set some cookies. Some of my pages use APIs - such as YouTube, LinkedIn, Google Fonts, Google Maps, Mapbox, Spotify, Jetpack, Twitter, Facebook &c.. Such plugins may set the odd cookie.

Close