Skocz do zawartości
Zaloguj się, aby obserwować  
Saint

[WordPress] custom fields i wproadzenie zależności w php

Polecane posty

Tworze sobie takie małe portfolio domen oparte na Wordpress i wykorzystuje przy tym custom fields. W jednym polu chcę użyć pewnej zależność w polu, które nazwałem status, a obecnie ma ono dwie funkcje tj.:

 

1. Jeśli pole jest puste na stronie wyświetli się "Złóż ofertę".

2. Jeśli pole zawiera jakikolwiek wpis np. "Sprzedane" na stronie wyświetli się "Sprzedane" ta funkcja chowa też formularz kontaktowy.

 

 

Kod tego fragmentu wygląda mniej więcej tak (panel administracyjny WP):

 

Funkcje motywu:

 

/* CUSTOM WRITE PANEL
/* ----------------------------------------------*/

function stream_capability() {
return (!!class_exists("SimpleXMLElement") AND !!function_exists("curl_init") ? TRUE : FALSE);
}

$new_meta_boxes =
array(
	"age" => array(
		"name" => "age",
		"type" => "text",
		"title" => "Archive.org:",
		"description" => "wiek domeny w Archive.org"),
	"inkeywords" => array(
		"name" => "inkeywords",
		"type" => "text",
		"title" => "Słowa kluczowe:",
		"description" => "słowa kluczowe"),
	"reg" => array(
		"name" => "reg",
		"type" => "text",
		"title" => "Rejestrator:",
		"description" => "Nazwa rejestratora, u którego domena została zarejestrowana"),
	"regdate" => array(
		"name" => "regdate",
		"type" => "text",
		"title" => "Data rejestracji:",
		"description" => "Kiedy domena została zarejestrowana"),
	"pr" => array(
		"name" => "pr",
		"type" => "text",
		"title" => "PageRank domeny:",
		"description" => "PageRank domeny w Google"),
	"alexa" => array(
		"name" => "alexa",
		"type" => "text",
		"title" => "Ranking Alexa:",
		"description" => "Pozycja w rankingu Alexa"),
	"site" => array(
		"name" => "site",
		"type" => "text",
		"title" => "Zaindeksowane strony:",
		"description" => "Ilość zindeksowanych stron w Google"),
	"link" => array(
		"name" => "link",
		"type" => "text",
		"title" => "Zaindeksowane odnośniki:",
		"description" => "Ilość zindeksowanych odnośników w Yahoo"),
	"authinfo" => array(
		"name" => "authinfo",
		"type" => "text",
		"title" => "AuthInfo:",
		"description" => "Czy kod AuthInfo jest dostępny z domeną?"),
	"status" => array(
		"name" => "status",
		"type" => "text",
		"title" => "Status:",
		"description" => "Status."),
	"price" => array(
		"name" => "price",
		"type" => "text",
		"title" => "Cena domeny:",
		"description" => "Cena za którą ma być sprzedana domena. Pozostawienie pustego pola spowoduje brak pola 'Cena' na stronie domeny."),

);

function new_meta_boxes() {
global $post, $new_meta_boxes;

foreach($new_meta_boxes as $meta_box) {
	$meta_box_value = get_post_meta($post->ID, $pre.'_value', true);

	if($meta_box_value == "")
		$meta_box_value = $meta_box['std'];

	echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
	echo'<h4 style="margin:0 0 5px 0;">'.$meta_box['title'].' <span style="font-weight:normal;font-size:9px;color:#666;">('.$meta_box['description'].')</span></h4>';
	echo'<input type="text" name="'.$meta_box['name'].'" value="'.get_post_meta($post->ID, $meta_box['name'], true).'" size="55" /><br />';
	echo'<p style="margin:0 0 10px 0;"></p>';
}
}

function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
	add_meta_box( 'new-meta-boxes', 'Custom Field Data', 'new_meta_boxes', 'post', 'normal', 'high' );
}
}

function save_postdata( $post_id ) {
global $post, $new_meta_boxes;

foreach($new_meta_boxes as $meta_box) {
	// Verify
	if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
		return $post_id;
	}

	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_id ))
			return $post_id;
	} else {
		if ( !current_user_can( 'edit_post', $post_id ))
			return $post_id;
	}

	$data = $_POST[$meta_box['name']];

	if(get_post_meta($post_id, $meta_box['name']) == "")
		add_post_meta($post_id, $meta_box['name'], $data, true);
	elseif($data != get_post_meta($post_id, $pre.'_value', true))
		update_post_meta($post_id, $meta_box['name'], $data);
	elseif($data == "")
		delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
}
}

 

Fragment dotyczący pola "status":

 

<tr class="bg1"><td>Status:</td><td><? $values = get_post_custom_values("status"); if ( is_array($values) ){ ?><span class="status"><?php $values = get_post_custom_values("status"); echo $values[0]; ?></span><? } else{ ?>Złóż ofertę<?php }?></td></tr>

 

Fragment dotyczący wyświetlania formularza kontaktowego:

 

</div><div class="contentboxbottom"></div>

<div class="clear"></div>

<? $values = get_post_custom_values("status"); if ( is_array($values) ){} else{ ?>

<div class="contentboxhead">Złóż ofertę kupna domeny <strong><?php the_title(); ?></strong></div>
<div class="contentboxinside">
<?php 
//If the form is submitted
if(isset($_POST['submitted'])) {

//Check to see if the honeypot captcha field was filled in
if(trim($_POST['checking']) !== '') {
	$captchaError = true;
} else {

	//Check to make sure that the name field is not empty
	if(trim($_POST['contactName']) === '') {
		$nameError = '<br /><span class="err">Podaj osobę kontaktową</span>';
		$hasError = true;
	} else {
		$name = trim($_POST['contactName']);
	}

	//Check to make sure that the name field is not empty
	if(trim($_POST['offer']) === '') {
		$offerError = '<br /><span class="err">Podaj ofertę w PLN</span>';
		$hasError = true;
	} else {
		$offer = trim($_POST['offer']);
	}

	//Check to make sure sure that a valid email address is submitted
	if(trim($_POST['email']) === '')  {
		$emailError = '<br /><span class="err">Wpisz adres e-mail</span>';
		$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$emailError = '<br /><span class="err">Wpisany adres e-mail nie jest prawidłowy</span>';
		$hasError = true;
	} else {
		$email = trim($_POST['email']);
	}

	//Check to make sure comments were entered	
	if(trim($_POST['comments']) === '') {
		$commentError = '<br /><span class="err">Wpisz treść wiadomości</span>';
		$hasError = true;
	} else {
		if(function_exists('stripslashes')) {
			$comments = stripslashes(trim($_POST['comments']));
		} else {
			$comments = trim($_POST['comments']);
		}
	}

	//If there is no error, send the email
	if(!isset($hasError)) {

		$emailTo = 'brokerdomen@g.pl';
		$domain = get_the_title();
		$subject = "You have an offer for $domain";
		$sendCopy = trim($_POST['sendCopy']);
		$body = "Name: $name \n\nEmail: $email \n\nOffer: $offer\n\nComments: $comments\n\n---------\nYour Site Name";
		$headers = 'From: Your Site Name <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

		mail($emailTo, $subject, $body, $headers);

		if($sendCopy == true) {
			$subject = 'Your Site Name';
			$headers = 'From: Your Name <you@yourdomainname.com>';
			mail($email, $subject, $body, $headers);
		}

		$emailSent = true;

	}
}
} ?>


<?php if(isset($emailSent) && $emailSent == true) { ?>

<div class="success">
	<h2>Dziękujemy, <?=$name;?></h2>
	<p>Twoja oferta została wysłana. Skontaktujemy się z Tobą niebawem.</p>
</div>

<?php } else { ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

	<?php if(isset($hasError) || isset($captchaError)) { ?>
	<?php } ?>

	<form action="<?php the_permalink(); ?>" id="contactForm" method="post">

		<div class="forms">
			<p><input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="text_input" /> <label for="author">Osoba kontaktowa</label>
				<?php if($nameError != '') { ?>
					<span class="error"><?=$nameError;?></span> 
				<?php } ?>
			</p>

			<p><input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="text_input" /> <label for="email">E-mail</label>
				<?php if($emailError != '') { ?>
					<span class="error"><?=$emailError;?></span>
				<?php } ?>
			</p>

			<p>
				<input type="text" name="offer" id="offer" value="<?php if(isset($_POST['offer'])) echo $_POST['offer'];?>" class="text_input" /> <label for="offer">Proponowana cena</label>
				<?php if($offerError != '') { ?>
					<span class="error"><?=$offerError;?></span>
				<?php } ?>
			</p>				

			<p class="textarea"><textarea name="comments" id="commentsText" rows="5" cols="30" class="text_input text_area"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
				<?php if($commentError != '') { ?>
					<?=$commentError;?>
				<?php } ?>
			</p>
			<p class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit" class="submit"></button></p>
		</div>
	</form>

	<?php endwhile; ?>
<?php endif; ?>
<?php } ?>

</div><div class="contentboxbottom"></div>
<?php }?>

 

To co chce uzyskać to:

 

1. Jeśli pole jest puste lub zawiera "Złóż ofertę" na stronie wyświetli się "Złóż ofertę" (ta funkcja teoretycznie działa w tym co mam teraz jednak tylko jeśli pole jest puste).

2. Jeśli pole zawiera "Sprzedane" na stronie wyświetli się "Sprzedane" ta funkcja chowa też formularz kontaktowy (ta funkcja działa w tym co mam teraz).

3. Jeśli pole zawiera "Kup teraz" na stronie wyświetli się "Kup teraz" ta funkcja powinna chować formularz kontaktowy.

4. Jeśli pole zawiera "Kup teraz / Złóż ofertę" na stronie wyświetli się "Kup teraz / Złóż ofertę" z formularzem kontaktowym.

5. Jeśli pole zawiera "Aukcja" na stronie wyświetli się "Aukcja" jako link czyli:

<a href="http://http://www.aftermarket.pl/domena/<?php the_title(); ?>" target="_blank">Aukcja</a>

 

Kombinowałem i tak pomieszałem, że przekombinowałem więc nie chce mącić i będę bardzo wdzięczny jeśli ktoś będzie w stanie mi pomóc rozwiązać ten problem. Z góry dziękuję.

 

PS.: Namacalnie od strony usera można zobaczyć tego "potwora" TU ;).

Udostępnij ten post


Link to postu
Udostępnij na innych stronach
Zaloguj się, aby obserwować  

×