0

Hello i have create this script to see a saint on each different day and in relationship current day.

I use on header of template and work good:

           <?php  
            //Date parameter
            $currentDay = date('j');
            $currentMonth = date('n');
            //wp query to show saints name 
            $con = mysqli_connect("localhost","mydb","mypassword","myuser");
            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
            $sql = "select * from santi where numero_giorno= ".$currentDay." and numero_mese= ".$currentMonth;
            $result = mysqli_query($con, $sql);
            if (mysqli_affected_rows($con)>0) {
                while($row = $result->fetch_assoc()){
        ?>
        <span id="txtSaintName"><?php echo  $row['nome']?></span>
        <?php
                }
                mysqli_close($con);
            }
        ?>

Now i need to trasform this in shortcode to use in a widget.

I try to do this but if i copy shortcode on page or widget and try to see i se only the number of current day and not the text. Where wrong?

<?php 

add_shortcode("separatore", "separatore_html_render");  

function separatore_html_render( $atts ) {  
return                <?php  
                //Date parameter
                $currentDay = date('j');
                $currentMonth = date('n');
                //wp query to show saints name 
                $con = mysqli_connect("localhost","mydb","mypassword","myuser");
                if (mysqli_connect_errno()) {
                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
                $sql = "select * from santi where numero_giorno= ".$currentDay." and numero_mese= ".$currentMonth;
                $result = mysqli_query($con, $sql);
                if (mysqli_affected_rows($con)>0) {
                    while($row = $result->fetch_assoc()){
            ?>
            <span id="txtSaintName"><?php echo  $row['nome']?></span>
            <?php
                    }
                    mysqli_close($con);
                }
            ?>;  
}  
0

Try this use your desired name for function names :

function main_func_name() {

    //Date parameter
    $currentDay = date('j');
    $currentMonth = date('n');
    //wp query to show saints name 
    $con = mysqli_connect("localhost", "mydb", "mypassword", "myuser");
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql = "select * from santi where numero_giorno= " . $currentDay . " and numero_mese= " . $currentMonth;
    $result = mysqli_query($con, $sql);
    if (mysqli_affected_rows($con) > 0) {
        while ($row = $result->fetch_assoc()) {
            ?>
                         <span id="txtSaintName"><?php echo $row['nome'] ?></span>
            <?php
        }
        mysqli_close($con);
    }
}

add_shortcode('shortcode_name', 'function_shortcode');

function function_shortcode() {
    ob_start();
    main_func_name();
    return ob_get_clean();
}
?>
  • Yeah! This work. I have only a problem with special characthers. I try to use $sql = "select * from frasi_padre_pio where numero_giorno= " . $currentDay . " and numero_mese= " . $currentMonth; $result = mysqli_query($con, $sql, "utf8"); But give me error :( – skifast Feb 15 '17 at 13:37
  • can you show me the error plz? – Arsalan Mithani Feb 15 '17 at 14:53
  • Hello i have this error Warning: mysqli_query() expects parameter 3 to be long, string given in /home/mysite/public_html/wp-content/themes/NewTheme/functions.php on line 379 – skifast Feb 23 '17 at 15:46
  • query should be like : `$sql = "SELECT * FROM santi WHERE numero_giorno = " . $currentDay . " AND numero_mese = " . $currentMonth . "; & i cannot see your line 379 in functions.php – Arsalan Mithani Feb 23 '17 at 16:00
  • Yes but the problem is that i can't display special character. I need insert the character (utf8) to display text correctly – skifast Feb 23 '17 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.